Green
Green

Reputation: 30805

What does symbol sequence "#$" mean in Ruby and where to use it?

There is an example in the book:

"Seconds/day: #{24*60*60}" # => Seconds/day: 86400
"#{'Ho! '*3}Merry Christmas!" # => Ho! Ho! Ho! Merry Christmas!
"This is line #$." # => This is line 3

But when I try to implement the third line's symbol #$ it in a separate file, it prints smth strange. Here's my file str2.rb:

puts "Hello, World #$."
puts "Hello, World #$"
puts "#$"

Now I run it (in Win XP console):

C:\ruby\sbox>ruby str2.rb
Hello, World 0
Hello, World ["enumerator.so", "C:/RailsInstaller/Ruby1.9.3/lib/ruby/1.9.1/i386-mingw32/enc/encdb.so", "C:/Rai
lsInstaller/Ruby1.9.3/lib/ruby/1.9.1/i386-mingw32/enc/windows_1251.so", "C:/RailsInstaller/Ruby1.9.3/lib/ruby/
1.9.1/i386-mingw32/enc/trans/transdb.so", "C:/RailsInstaller/Ruby1.9.3/lib/ruby/site_ruby/1.9.1/rubygems/defau
lts.rb", "C:/RailsInstaller/Ruby1.9.3/lib/ruby/1.9.1/i386-mingw32/rbconfig.rb", "C:/RailsInstaller/Ruby1.9.3/l
ib/ruby/site_ruby/1.9.1/rubygems/deprecate.rb", "C:/RailsInstaller/Ruby1.9.3/lib/ruby/site_ruby/1.9.1/rubygems
/exceptions.rb", "C:/RailsInstaller/Ruby1.9.3/lib/ruby/site_ruby/1.9.1/rubygems/defaults/operating_system.rb",
 "C:/RailsInstaller/Ruby1.9.3/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb", "C:/RailsInstaller/Ruby1.9
.3/lib/ruby/site_ruby/1.9.1/rubygems.rb", "C:/RailsInstaller/Ruby1.9.3/lib/ruby/1.9.1/i386-mingw32/enc/utf_16l
e.so", "C:/RailsInstaller/Ruby1.9.3/lib/ruby/1.9.1/i386-mingw32/enc/trans/utf_16_32.so", "C:/RailsInstaller/Ru
by1.9.3/lib/ruby/1.9.1/i386-mingw32/enc/trans/single_byte.so"]
puts

I've found that #$. (the period is mandatory) shows the line number only in Interactive Ruby Console. Used in file it produces 0 on any line. But why all that text is printed if I use symbols like this "#$" \n "#$"?

Also such a code in a file:

puts "Hello, World #$" ## without period at the end

produces such an error:

C:\ruby\sbox>ruby str2.rb
str2.rb:3: unterminated string meets end of file

What does #$ mean? Where and how to use it?

Upvotes: 2

Views: 191

Answers (2)

Bungus
Bungus

Reputation: 602

In Ruby a global variables are defined using dollar sign.

$foo = "bar"

There are some pre-defined globals such as

# last line number seen by interpreter
$.

I think you are just missing the period. You can insert a variable into a string using -

"line #{$.}"

or shorthand

"line #$."

Upvotes: 0

Andrew Marshall
Andrew Marshall

Reputation: 96954

"#$." is shorthand for "#{$.}", or the interpolation of a global variable. Similarly there is #@ for an instance variable & #@@ for a class variable.

The problem with what you have is that the second " in "#$" is not interpreted as the closing quote for the string, but instead as part of the global variable name being interpolated ($"). To make it more clear how your code is actually being interpreted, I’ll use string literals in place of what Ruby thinks are string delimiters:

puts %(Hello, World #$.)
puts %(Hello, World #$"
puts )#$"

As you can see, this is where the array printed comes from (it’s the contents of $") as well as the “puts” string at the end. The #$" at the end of your code is interpreted as a comment. (Note that the second string is spanning—and including—the newline between lines two and three.)

If you actually want to print #$ as a string, you have to escape part of it or use a single-quoted string:

  • "\#$" #=> "#$"
  • "#\$" #=> "#$"
  • '#$' #=> "#$"

Simply placing #$ in an interpolated string without escaping is not valid, as can be seen by using string literals:

%(#$)  #=> #<SyntaxError: (eval):2: syntax error, unexpected $undefined
       #   %(#$)
       #      ^>

Upvotes: 6

Related Questions