Trouble parsing against a hash

Hello,
I get the following error message from RUBY when I try to run the script
shown below. Sorry, the hash is huge.

I need to match the beginning of file names with the keys in a hash. If
there’s a match, then I need to pull the value for that matching key.
That’s all really.

Thanks,
Peter

Error:
test2.rb:7: parse error, unexpected kEND, expecting ‘}’

Ruby Script:
1 require ‘FileUtils’
2 Dir.chdir(‘u:/indexes’)
3 fileshash = { :aacmc7p => ‘aacm00’, :acm059p => ‘bacm00’, :acm061p =>
‘bacm00’, :acmc59p => ‘bacm00’, :acmt59p => ‘bacm00’, :adc064p =>
‘adam00’, :adm064p => ‘adam00’, :adm065p => ‘adam00’, :apr005p =>
‘tmap00’, :atc071p => ‘atrr00’, :bac054p => ‘bnkr00’, :bkc031p =>
‘bblr00’, :bpc061p => ‘btm000’, :cal158p => ‘clas00’, :car125p =>
‘care00’, :cbncc7p => ‘cbnc00’, :cgmprp_ => ‘cglw00’, :chc074p =>
‘chem00’, :chc075p => ‘chem00’, :clc017p => ‘clr000’, :crc074p =>
‘crl000’, :ctl157p => ‘ctlr00’, :dec079p => ‘der000’, :dlc080p =>
‘dlr000’, :dtc069p => ‘dt0000’, :edc131p => ‘eddg00’, :edr094p =>
‘edr000’, :edt131p => ‘eddg00’, :eeomc7p => ‘eeom00’, :eip066p =>
‘eplr00’, :emgc15p => ‘emgd00’, :erc023p => ‘er0000’, :ercr25p =>
‘er0000’, :exe155p => ‘exer00’, :fcc093p => ‘fcr000’, :flc046p =>
‘flr000’, :gec099p => ‘gerr00’, :hac076p => ‘hzrf00’, :hcc201p =>
‘hccg00’, :hcci01p => ‘hccg00’, :hcp030p => ‘hce000’, :hct201p =>
‘hccg00’, :hcti01p => ‘hccg00’, :hfr085p => ‘hfra00’, :hla020p =>
‘hblw00’, :hlb020p => ‘hblw00’, :hlr036p => ‘hlr000’, :hltc20p =>
‘hblw00’, :hpr042p => ‘hppr00’, :hrr081p => ‘hrr000’, :ierc77p =>
‘iner00’, :ierc78p => ‘iner00’, :itc010p => ‘itr000’, :itc101p =>
‘itr000’, :jsc047p => ‘josh00’, :lrc013p => ‘lrr000’, :lrc096p =>
‘lrwk00’, :lrfc74p => ‘lrr000’, :lrfl21p => ‘lrr000’, :lrgc68p =>
‘lrr000’, :lrnm21p => ‘lrr000’, :lrrc21p => ‘lrr000’, :lrrc22p =>
‘lrr000’, :lrrc24p => ‘lrr000’, :lrrc37p => ‘lrr000’, :lrrc49p =>
‘lrr000’, :lrrc74p => ‘lrr000’, :lrrc82p => ‘lrr000’, :lrrmc6p =>
‘lrr000’, :lrrt37p => ‘lrr000’, :lrt013p => ‘lrr000’, :lrtc68p =>
‘lrr000’, :lwc001p => ‘lw0000’, :lwc002p => ‘lw0000’, :lwca85p =>
‘lw0000’, :lwgc86p => ‘lw0000’, :lwsc85p => ‘lw0000’, :mal058p =>
‘malr00’, :mcc019p => ‘mcr000’, :mopc68p => ‘mopc00’, :mopd68p =>
‘mopc00’, :mopec8p => ‘mopc00’, :mopt68p => ‘mopc00’, :mpc067p =>
‘mopc00’, :mrl150p => ‘mrlr00’, :osbc35p => ‘oshr00’, :osc022p =>
‘oshr00’, :pec029p => ‘pen000’, :pgc116p => ‘pygd00’, :pgc117p =>
‘pygd00’, :plr126p => ‘plir00’, :psc044p => ‘pslr00’, :pslc45p =>
‘pslr00’, :psvc45p => ‘pslr00’, :ptcc54p => ‘ptcj00’, :pvl162p =>
‘pvlr00’, :rtc129p => ‘rkpg00’, :src087p => ‘srlr00’, :tlc100p =>
‘txlr00’, :tmc030p => ‘tmeg00’, :tmc032p => ‘tmwr00’, :tmc040p =>
‘tmf000’, :tmc045p => ‘tmtr00’, :tmc050p => ‘tmfp00’, :tmc060p =>
‘tmcp00’, :tmc065p => ‘tmpl00’, :tmc070p => ‘tmeg00’, :tmc080p =>
‘tmi000’, :tmc090p => ‘tmre00’, :tmc100p => ‘tm0000’, :tmcc20p =>
‘tmcp00’, :tmf186p => ‘tmf000’, :tmi185p => ‘tmus00’, :tmic20p =>
‘tmus00’, :tmrc20p => ‘tmre00’, :tms190p => ‘stat00’, :tmsp50p =>
‘stat00’, :tmt055p => ‘tmtr00’, :uln007p => ‘ulr000’, :wcr006p =>
‘wcr000’, :whl164p => ‘wplr00’, :ad137b1p => ‘lrr000’, :ad137b2p =>
‘lrr000’, :adc095 => ‘lrr000’, :eb062b1p => ‘ebcd00’, :eb161b1p =>
‘ebcd00’, :eb161b2p => ‘ebcd00’, :eb161b4p => ‘ebcd00’, :eb960b1p =>
‘ebcd00’, :eb960b2p => ‘ebcd00’, :er022b1p => ‘er0000’, :er022b2p =>
‘er0000’, :er121b1p => ‘er0000’, :er121b2p => ‘er0000’, :er121b5p =>
‘er0000’, :er901b1p => ‘er0000’, :er901b2p => ‘er0000’, :er901b3p =>
‘er0000’, :fe015b1p => ‘lrr000’, :ie005b1p => ‘lrr000’, :ie105b1p =>
‘lrr000’, :it071b1p => ‘itr000’, :it171b1p => ‘itr000’, :it171b3p =>
‘itr000’, :lr315b1p => ‘lrr000’, :lr315b2p => ‘lrr000’, :lrr084 =>
‘lrr000’, :lrr089 => ‘lrr000’, :lrr090 => ‘lrr000’, :lrr095 => ‘lrr000’,
:lrr096 => ‘lrr000’, :lrr122 => ‘lrr000’, :ml032b1p => ‘med000’,
:ml032b1t => ‘med000’, :ml032b2p => ‘med000’, :ml032b2t => ‘med000’,
:ml131b1p => ‘med000’, :ml131b1t => ‘med000’, :ml131b2t => ‘med000’,
:ml930b1p => ‘med000’, :ml930b1t => ‘med000’, :os042b2p => ‘oshr00’,
:os142b2p => ‘oshr00’, :osh034 => ‘osh200’, :pq181b1p => ‘pq0000’,
:pq181b1t => ‘pq0000’, :pq181b2p => ‘pq0000’, :pq181b3t => ‘pq0000’,
:pq192b1p => ‘pq0000’, :pq192b1t => ‘pq0000’, :pq192b3p => ‘pq0000’,
:wh650b1p => ‘lrr000’, :wh650b2p => 'lrr000 }

4 psfiles = Dir.glob(".ps")
5 psfiles.each do |psfile|
6 filehash.each { |key, value| puts “#{value}” if
File.fnmatch(’#{key}
’, #{psfile}) }
7 end

You’re missing a closing quote in the last value of the hash:

:wh650b1p => ‘lrr000’, :wh650b2p => 'lrr000 }

should be :

:wh650b1p => ‘lrr000’, :wh650b2p => ‘lrr000’ }

Jeff
softiesonrails.com

Jeff C. wrote:

You’re missing a closing quote in the last value of the hash:

:wh650b1p => ‘lrr000’, :wh650b2p => 'lrr000 }

should be :

:wh650b1p => ‘lrr000’, :wh650b2p => ‘lrr000’ }

Jeff
softiesonrails.com

Thanks, Jeff. I did find that problem already and I fixed it. I also had
a typo in filehash.each. It should be fileshash.each. Fixed that, too.
But, anyway, it still ain’t working. I’m getting the following error:

test2.rb:9: parse error, unexpected kEND, expecting $

where line 9 is now the last line of the script, the “end” line.

On Aug 14, 2006, at 11:42 AM, Peter B. wrote:

Hello,

Hello.

I get the following error message from RUBY when I try to run the
script
shown below. Sorry, the hash is huge.

Just FYI, the language is Ruby, not RUBY. :wink:

I found two problems with your code…

:wh650b1p => ‘lrr000’, :wh650b2p => 'lrr000 }

That very last Hash entry is missing a closing quote.

File.fnmatch(’#{key}*’, #{psfile}) }

I’m not 100% sure what was intended here, but the # character starts
a comment, causing the rest of the line, including a needed ) and },
to be ignored.

Hope that helps.

James Edward G. II

On Tue, 15 Aug 2006, Peter B. wrote:

Hello,
I get the following error message from RUBY when I try to run the script
shown below. Sorry, the hash is huge.

I need to match the beginning of file names with the keys in a hash. If
there’s a match, then I need to pull the value for that matching key.
That’s all really.

Thanks,
Peter

get a good highlighting editor like vim and you’ll see these errors

require ‘FileUtils’
^ ^
^ ^
^ ^
f u

:wh650b1p => ‘lrr000’, :wh650b2p => 'lrr000 }
^
^
^

if File.fnmatch(’#{key}*’, #{psfile} ) }
^ ^
^ ^
^ ^
" "

and you probably meant "#{ key }" not '#{ key }’ since only the
former
expands the variable (note double vs single quotes).

btw. with huge hashes like this it’s far easier to maintain an inlined
yaml
doc of the hash that ruby source. for instance, this is long, but quite
easy
to read, maintain, and search:

harp:~ > cat a.rb

require ‘fileutils’
require ‘yaml’

fileshash = YAML.load DATA.read

Dir.chdir ‘u:/indexes’

paths = Dir.glob “*.ps”

paths.each do |path|
filehash.each{|k,v| puts “#{ v }” if File.fnmatch("#{ k }*", “#{ path
}”) }
end

END

aacmc7p: aacm00
acm059p: bacm00
acm061p: bacm00
acmc59p: bacm00
acmt59p: bacm00
adc064p: adam00
adm064p: adam00
adm065p: adam00
apr005p: tmap00
atc071p: atrr00
bac054p: bnkr00
bkc031p: bblr00
bpc061p: btm000
cal158p: clas00
car125p: care00
cbncc7p: cbnc00
cgmprp_: cglw00
chc074p: chem00
chc075p: chem00
clc017p: clr000
crc074p: crl000
ctl157p: ctlr00
dec079p: der000
dlc080p: dlr000
dtc069p: dt0000
edc131p: eddg00
edr094p: edr000
edt131p: eddg00
eeomc7p: eeom00
eip066p: eplr00
emgc15p: emgd00
erc023p: er0000
ercr25p: er0000
exe155p: exer00
fcc093p: fcr000
flc046p: flr000
gec099p: gerr00
hac076p: hzrf00
hcc201p: hccg00
hcci01p: hccg00
hcp030p: hce000
hct201p: hccg00
hcti01p: hccg00
hfr085p: hfra00
hla020p: hblw00
hlb020p: hblw00
hlr036p: hlr000
hltc20p: hblw00
hpr042p: hppr00
hrr081p: hrr000
ierc77p: iner00
ierc78p: iner00
itc010p: itr000
itc101p: itr000
jsc047p: josh00
lrc013p: lrr000
lrc096p: lrwk00
lrfc74p: lrr000
lrfl21p: lrr000
lrgc68p: lrr000
lrnm21p: lrr000
lrrc21p: lrr000
lrrc22p: lrr000
lrrc24p: lrr000
lrrc37p: lrr000
lrrc49p: lrr000
lrrc74p: lrr000
lrrc82p: lrr000
lrrmc6p: lrr000
lrrt37p: lrr000
lrt013p: lrr000
lrtc68p: lrr000
lwc001p: lw0000
lwc002p: lw0000
lwca85p: lw0000
lwgc86p: lw0000
lwsc85p: lw0000
mal058p: malr00
mcc019p: mcr000
mopc68p: mopc00
mopd68p: mopc00
mopec8p: mopc00
mopt68p: mopc00
mpc067p: mopc00
mrl150p: mrlr00
osbc35p: oshr00
osc022p: oshr00
pec029p: pen000
pgc116p: pygd00
pgc117p: pygd00
plr126p: plir00
psc044p: pslr00
pslc45p: pslr00
psvc45p: pslr00
ptcc54p: ptcj00
pvl162p: pvlr00
rtc129p: rkpg00
src087p: srlr00
tlc100p: txlr00
tmc030p: tmeg00
tmc032p: tmwr00
tmc040p: tmf000
tmc045p: tmtr00
tmc050p: tmfp00
tmc060p: tmcp00
tmc065p: tmpl00
tmc070p: tmeg00
tmc080p: tmi000
tmc090p: tmre00
tmc100p: tm0000
tmcc20p: tmcp00
tmf186p: tmf000
tmi185p: tmus00
tmic20p: tmus00
tmrc20p: tmre00
tms190p: stat00
tmsp50p: stat00
tmt055p: tmtr00
uln007p: ulr000
wcr006p: wcr000
whl164p: wplr00
ad137b1p: lrr000
ad137b2p: lrr000
adc095: lrr000
eb062b1p: ebcd00
eb161b1p: ebcd00
eb161b2p: ebcd00
eb161b4p: ebcd00
eb960b1p: ebcd00
eb960b2p: ebcd00
er022b1p: er0000
er022b2p: er0000
er121b1p: er0000
er121b2p: er0000
er121b5p: er0000
er901b1p: er0000
er901b2p: er0000
er901b3p: er0000
fe015b1p: lrr000
ie005b1p: lrr000
ie105b1p: lrr000
it071b1p: itr000
it171b1p: itr000
it171b3p: itr000
lr315b1p: lrr000
lr315b2p: lrr000
lrr084: lrr000
lrr089: lrr000
lrr090: lrr000
lrr095: lrr000
lrr096: lrr000
lrr122: lrr000
ml032b1p: med000
ml032b1t: med000
ml032b2p: med000
ml032b2t: med000
ml131b1p: med000
ml131b1t: med000
ml131b2t: med000
ml930b1p: med000
ml930b1t: med000
os042b2p: oshr00
os142b2p: oshr00
osh034: osh200
pq181b1p: pq0000
pq181b1t: pq0000
pq181b2p: pq0000
pq181b3t: pq0000
pq192b1p: pq0000
pq192b1t: pq0000
pq192b3p: pq0000
wh650b1p: lrr000
wh650b2p: lrr000

On 06-08-14, at 12:42, Peter B. wrote:

Error:
test2.rb:7: parse error, unexpected kEND, expecting ‘}’

[snip]

4 psfiles = Dir.glob(".ps")
5 psfiles.each do |psfile|
6 filehash.each { |key, value| puts “#{value}” if
File.fnmatch(’#{key}
’, #{psfile}) } # <-- that last } should be an
end, or the do above should be a {


Jeremy T.
[email protected]

“One serious obstacle to the adoption of good programming languages
is the notion that everything has to be sacrificed for speed. In
computer languages as in life, speed kills.” – Mike Vanier

On Aug 14, 2006, at 12:42 PM, Peter B. wrote:

File.fnmatch(’#{key}*’, #{psfile})

Your quotes are wrong. I think this is what you are looking for:

File.fnmatch("#{key}*", psfile)

On 06-08-14, at 14:39, Jeremy T. wrote:

there’s a match, then I need to pull the value for that matching key.
File.fnmatch(’#{key}*’, #{psfile}) } # <-- that last } should be
an end, or the do above should be a {

Bah disregard, still sleepy. Sorry for the noise.


Jeremy T.
[email protected]

“One serious obstacle to the adoption of good programming languages
is the notion that everything has to be sacrificed for speed. In
computer languages as in life, speed kills.” – Mike Vanier

Peter B. wrote:

where line 9 is now the last line of the script, the “end” line.

Please repost your newest version if you are still having issues.

Disregard if you got it all fixed up :slight_smile:

-Justin

unknown wrote:

On Tue, 15 Aug 2006, Peter B. wrote:

Hello,
I get the following error message from RUBY when I try to run the script
shown below. Sorry, the hash is huge.

I need to match the beginning of file names with the keys in a hash. If
there’s a match, then I need to pull the value for that matching key.
That’s all really.

Thanks,
Peter

harp:~ > cat a.rb

require ‘fileutils’
require ‘yaml’

fileshash = YAML.load DATA.read

Dir.chdir ‘u:/indexes’

paths = Dir.glob “*.ps”

paths.each do |path|
filehash.each{|k,v| puts “#{ v }” if File.fnmatch("#{ k }*", “#{ path
}”) }
end

END

aacmc7p: aacm00
acm059p: bacm00
acm061p: bacm00
acmc59p: bacm00
acmt59p: bacm00
adc064p: adam00
adm064p: adam00
adm065p: adam00
apr005p: tmap00
atc071p: atrr00
bac054p: bnkr00
bkc031p: bblr00
bpc061p: btm000
cal158p: clas00
car125p: care00
cbncc7p: cbnc00
cgmprp_: cglw00
chc074p: chem00
chc075p: chem00
clc017p: clr000
crc074p: crl000
ctl157p: ctlr00
dec079p: der000
dlc080p: dlr000
dtc069p: dt0000
edc131p: eddg00
edr094p: edr000
edt131p: eddg00
eeomc7p: eeom00
eip066p: eplr00
emgc15p: emgd00
erc023p: er0000
ercr25p: er0000
exe155p: exer00
fcc093p: fcr000
flc046p: flr000
gec099p: gerr00
hac076p: hzrf00
hcc201p: hccg00
hcci01p: hccg00
hcp030p: hce000
hct201p: hccg00
hcti01p: hccg00
hfr085p: hfra00
hla020p: hblw00
hlb020p: hblw00
hlr036p: hlr000
hltc20p: hblw00
hpr042p: hppr00
hrr081p: hrr000
ierc77p: iner00
ierc78p: iner00
itc010p: itr000
itc101p: itr000
jsc047p: josh00
lrc013p: lrr000
lrc096p: lrwk00
lrfc74p: lrr000
lrfl21p: lrr000
lrgc68p: lrr000
lrnm21p: lrr000
lrrc21p: lrr000
lrrc22p: lrr000
lrrc24p: lrr000

Wow. Thanks, unknown Guest. I didn’t do your YAML thing, but, I did use
your last fileshash.each routine, and that worked beautifully. I had
actually written a note to the forum a couple of weeks ago regarding
YAML, because, intuitively, it seemed to make sense for such a large
piece of data like mine. Now, forgive my ignorance, but, is your YAML
table set up in a separate file, or, is it literally in the same script?
I would like to know more about YAML.

Thanks.

James G. wrote:

On Aug 14, 2006, at 11:42 AM, Peter B. wrote:

Hello,

Hello.

I get the following error message from RUBY when I try to run the
script
shown below. Sorry, the hash is huge.

Just FYI, the language is Ruby, not RUBY. :wink:

I found two problems with your code…

:wh650b1p => ‘lrr000’, :wh650b2p => 'lrr000 }

That very last Hash entry is missing a closing quote.

File.fnmatch(’#{key}*’, #{psfile}) }

I’m not 100% sure what was intended here, but the # character starts
a comment, causing the rest of the line, including a needed ) and },
to be ignored.

Hope that helps.

James Edward G. II

Thanks, James. Sorry 'bout Ruby. I don’t understand why my pound sign is
a comment starter there, but, it’s not prior to that in the same line.
Isn’t that a legitimate pointer to a variable, #{psfile} ?

unknown wrote:

On Tue, 15 Aug 2006, Peter B. wrote:

Hello,
I get the following error message from RUBY when I try to run the script
shown below. Sorry, the hash is huge.

I need to match the beginning of file names with the keys in a hash. If
there’s a match, then I need to pull the value for that matching key.
That’s all really.

Thanks,
Peter

harp:~ > cat a.rb

require ‘fileutils’
require ‘yaml’

fileshash = YAML.load DATA.read

Dir.chdir ‘u:/indexes’

paths = Dir.glob “*.ps”

paths.each do |path|
filehash.each{|k,v| puts “#{ v }” if File.fnmatch("#{ k }*", “#{ path
}”) }
end

END

aacmc7p: aacm00
acm059p: bacm00
acm061p: bacm00
acmc59p: bacm00
acmt59p: bacm00
adc064p: adam00
adm064p: adam00

Wow. Thanks, unknown Guest. I didn’t do your YAML thing, but, I did use
your last fileshash.each routine, and that worked beautifully. I had
actually written a note to the forum a couple of weeks ago regarding
YAML, because, intuitively, it seemed to make sense for such a large
piece of data like mine. Now, forgive my ignorance, but, is your YAML
table set up in a separate file, or, is it literally in the same script?
I would like to know more about YAML.

Thanks.

Austin Z. wrote:

On 8/15/06, Peter B. [email protected] wrote:

Thanks, James. Sorry 'bout Ruby. I don’t understand why my pound sign is
a comment starter there, but, it’s not prior to that in the same line.
Isn’t that a legitimate pointer to a variable, #{psfile} ?

No. There’s no such thing as a pointer to a variable in Ruby; Ruby
variables don’t work that way (thank ghu). The construct #{var} is
used for interpolation, but only in regular expression and quoted
string constructs. That means it can be used with %r{#{var}},
/#{var}/, “#{var}”, %Q{#{var}}, %W{#{var}}, and heredocs with
interpolation (<<-“EOS” instead of <<-‘EOS’).

The #{} interpolation does not even have to be a variable. I could do:

puts “2 + 2 = #{2 + 2}”

Any legal expression may be in the interpolation, but it may cause
your syntax highlighting routines in your editor some grief if you get
too complex there. Some things do not need the braces, such as global
variables (#$LOAD_PATH) or instance variables (#@inst_var). When
you’re not in an interpolative mode, if you use the #{} (or #$ or #@)
you will simply get a commented rest of the line.

-austin

You mean, it’s because I was simply missing quotes around my “#{}”
expression that it was regarded as a comment? Well, I guess I can
certainly understand that. Thanks a lot.

On 8/15/06, Peter B. [email protected] wrote:

Thanks, James. Sorry 'bout Ruby. I don’t understand why my pound sign is
a comment starter there, but, it’s not prior to that in the same line.
Isn’t that a legitimate pointer to a variable, #{psfile} ?

No. There’s no such thing as a pointer to a variable in Ruby; Ruby
variables don’t work that way (thank ghu). The construct #{var} is
used for interpolation, but only in regular expression and quoted
string constructs. That means it can be used with %r{#{var}},
/#{var}/, “#{var}”, %Q{#{var}}, %W{#{var}}, and heredocs with
interpolation (<<-“EOS” instead of <<-‘EOS’).

The #{} interpolation does not even have to be a variable. I could do:

puts “2 + 2 = #{2 + 2}”

Any legal expression may be in the interpolation, but it may cause
your syntax highlighting routines in your editor some grief if you get
too complex there. Some things do not need the braces, such as global
variables (#$LOAD_PATH) or instance variables (#@inst_var). When
you’re not in an interpolative mode, if you use the #{} (or #$ or #@)
you will simply get a commented rest of the line.

-austin

On Aug 15, 2006, at 8:14 AM, Peter B. wrote:

You mean, it’s because I was simply missing quotes around my “#{}”
expression that it was regarded as a comment? Well, I guess I can
certainly understand that. Thanks a lot.

Right. But never use:

“#{var}”

because that’s just a confusing way to say:

var.to_s

And if the variable is already a String, you really just want:

var

James Edward G. II

On Aug 15, 2006, at 7:57 AM, Peter B. wrote:

James Edward G. II

Thanks, James. Sorry 'bout Ruby. I don’t understand why my pound
sign is
a comment starter there, but, it’s not prior to that in the same line.
Isn’t that a legitimate pointer to a variable, #{psfile} ?

The first occurrence in the line is inside a String:

‘#{key}*’

And I just realized that doesn’t do what you think it does either. :wink:

#{ … } allows you to interpolate variables inside a double-quoted
String. It means nothing special in a single-quoted String. Thus
you likely meant:

“#{key}*”

Outside of a String, # is Ruby’s line comment character which causes
Ruby to ignore the rest of the line. When you want a variable’s
contents, you just use the variable name. You probably intended the
second one to be:

psfile

Hope that helps.

James Edward G. II

James G. wrote:

On Aug 15, 2006, at 8:14 AM, Peter B. wrote:

You mean, it’s because I was simply missing quotes around my “#{}”
expression that it was regarded as a comment? Well, I guess I can
certainly understand that. Thanks a lot.

Right. But never use:

“#{var}”

because that’s just a confusing way to say:

var.to_s

And if the variable is already a String, you really just want:

var

James Edward G. II

OK. I’m a bit confused, though. I’ve been using #{var} for quite a while
now. I thought it was needed when getting a variable within a block. Is
it when it’s in double quotes that you suggest not using it?

On Aug 15, 2006, at 9:18 AM, Peter B. wrote:

OK. I’m a bit confused, though. I’ve been using #{var} for quite a
while
now. I thought it was needed when getting a variable within a
block. Is
it when it’s in double quotes that you suggest not using it?

As we have said, #{ … } is only used inside double-quoted Strings.
You don’t need it anywhere else.

You use it to interpolate some value into a String. For example:

“1 + 2 = #{1 + 2}”

or:

var = 2
“1, #{var}, 3, …”

When used like that, you are fine. That’s what it’s for.

However, if you ever type just that it a String, you didn’t need
the String:

“#{var}” # => same as var.to_s or just var, if it is a String

Hope that makes more sense.

James Edward G. II

Yup. That does make sense now. And, it’s an eye-opener for me. Your
words “only used” above are very significant. Thanks a lot.

On Tue, 15 Aug 2006, James Edward G. II wrote:

As we have said, #{ … } is only used inside double-quoted Strings. You
don’t need it anywhere else.

to clarify: you can’t use it anywhere else.

kind regards.

-a

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs