Code ok in ruby, but fails in irb

The following statement, with the newline, is ok with ruby but generates
a syntax error in irb:

"#{ begin "#{3}" end
}"

I expect that ruby and irb should have the same idea of what constitutes
legal code?

I encountered the problem while testing this function:

def maketable(infohash)
  "<table>#{
    infohash.collect{|k,v|
      "<tr><td>#{k}</td><td>#{v}</td></tr>"
    }.join
  }</table>"
end

Dump from test terminal:

sh-4.1$ ruby -v
ruby 1.8.7 (2011-02-18 patchlevel 334) [i386-linux]
sh-4.1$ cat x.rb
"#{ begin "#{3}" end
}"
sh-4.1$ ruby x.rb
sh-4.1$ irb
irb(main):001:0> "#{ begin "#{3}" end }"
=> "3"
irb(main):002:0> "#{ begin "#{3}" end
SyntaxError: compile error
(irb):2: syntax error, unexpected $end, expecting '}'
  from (irb):2

···

from :0

--
Posted via http://www.ruby-forum.com/.

I expect that ruby and irb should have the same idea of what
constitutes legal code?

Nope. And that's why I never use irb (nor similar toys in other
languages).

···

--
Posted via http://www.ruby-forum.com/\.

Works for me on 1.9, but not on 1.8

It's worth updating, IMO. 1.9 is faster, newer, better in almost every way.
There is already talk of EOL for 1.8, it won't be supported much longer.

···

On Sat, Jul 23, 2011 at 5:41 AM, Trygve Flathen <at.ruby-forum@flathen.net>wrote:

The following statement, with the newline, is ok with ruby but generates
a syntax error in irb:

"#{ begin "#{3}" end
}"

I expect that ruby and irb should have the same idea of what constitutes
legal code?

I encountered the problem while testing this function:

def maketable(infohash)
"<table>#{
   infohash.collect{|k,v|
     "<tr><td>#{k}</td><td>#{v}</td></tr>"
   }.join
}</table>"
end

Dump from test terminal:

sh-4.1$ ruby -v
ruby 1.8.7 (2011-02-18 patchlevel 334) [i386-linux]
sh-4.1$ cat x.rb
"#{ begin "#{3}" end
}"
sh-4.1$ ruby x.rb
sh-4.1$ irb
irb(main):001:0> "#{ begin "#{3}" end }"
=> "3"
irb(main):002:0> "#{ begin "#{3}" end
SyntaxError: compile error
(irb):2: syntax error, unexpected $end, expecting '}'
from (irb):2
from :0

--
Posted via http://www.ruby-forum.com/\.

Josh Cheek wrote in post #1012566:

It's worth updating, IMO. 1.9 is faster, newer, better in almost every
way.

... apart from the train wreck of Encodings (IMO - just giving a
different point of view).

···

--
Posted via http://www.ruby-forum.com/\.