Skip to content

Instantly share code, notes, and snippets.

@veryhappythings
Created January 5, 2012 21:05
Show Gist options
  • Save veryhappythings/1567277 to your computer and use it in GitHub Desktop.
Save veryhappythings/1567277 to your computer and use it in GitHub Desktop.
Why can't you puts a hash directly?
[serious_business (master)↑⚡] ➔ irb
1.9.3p0 :001 > puts {3 => 4}
SyntaxError: (irb):1: syntax error, unexpected tASSOC, expecting '}'
puts {3 => 4}
^
from /Users/mac/.rvm/rubies/ruby-1.9.3-p0/bin/irb:16:in `<main>'
1.9.3p0 :002 > p {3 => 4}
SyntaxError: (irb):2: syntax error, unexpected tASSOC, expecting '}'
p {3 => 4}
^
from /Users/mac/.rvm/rubies/ruby-1.9.3-p0/bin/irb:16:in `<main>'
1.9.3p0 :003 > h = {3 => 4}
=> {3=>4}
1.9.3p0 :004 > puts h
{3=>4}
=> nil
1.9.3p0 :005 >
@lukaszkorecki
Copy link

try:

puts({3 =>4})

Compare to this:

puts { "lol" }

or with:

puts { f='lol'; f += ' wat' }

...and now you know why :-)

@chrismytton
Copy link

It's interpreting the hash as a block argument to the puts method. Try this:

puts({3 => 4})

@veryhappythings
Copy link
Author

Ah ha, I see! For some reason it didn't twig that it was interpreting it as a block. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment