# Hash vs block

**URL:** <https://www.ruby-forum.com/t/hash-vs-block/233966>\
**Category:** Ruby\
**Created:** [January 26, 2015, 6:39pm UTC](https://www.ruby-forum.com/t/hash-vs-block/233966 "2015-01-26T18:39:46Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![addis\_a](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/c4cdca/32.png) [@addis\_a](https://www.ruby-forum.com/u/addis_a)\
**Post date:** [January 26, 2015, 6:39pm UTC](https://www.ruby-forum.com/t/hash-vs-block/233966/1 "2015-01-26T18:39:46Z")

</div>

why is it that as soon as you call puts it turns the hash into a block?

# hash

{ 1 =\> 1}

# block

puts {1=\>1}

I feel like this should still be a hash, there is an example on the ruby  
docs that doesnt work that showcases the difference here:

require ‘json’

my\_hash = {:hello =\> “goodbye”}  
puts JSON.generate(my\_hash)

This does not work, yet its documented @

> **[Module: JSON (Ruby 2.0.0)](https://ruby-doc.org/stdlib-2.0.0/libdoc/json/rdoc/JSON.html)**
>
> Module : JSON - Ruby 2.0.0

---

<div class="post-metadata">

**Author:** ![hpre](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/ac91a4/32.png) [@hpre](https://www.ruby-forum.com/u/hpre)\
**Post date:** [January 26, 2015, 9:26pm UTC](https://www.ruby-forum.com/t/hash-vs-block/233966/2 "2015-01-26T21:26:22Z")

</div>

I’m not quite sure what you are asking, but the example you link to is,  
as you write, a bit wrong. Try it in IRB without the puts, and you’ll  
see it will output the expected JSON string

irb(main):001:0\> require ‘json’  
=\> true  
irb(main):002:0\> my\_hash = {:hello =\> “goodbye”}  
=\> {:hello=\>“goodbye”}  
irb(main):003:0\> JSON.generate(my\_hash)  
=\> “{“hello”:“goodbye”}”

---

<div class="post-metadata">

**Author:** ![hpre](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/ac91a4/32.png) [@hpre](https://www.ruby-forum.com/u/hpre)\
**Post date:** [January 26, 2015, 9:45pm UTC](https://www.ruby-forum.com/t/hash-vs-block/233966/3 "2015-01-26T21:45:40Z")

</div>

Sorry, I did a poor job of explaining. The lack of clarity for me is  
where a hash turns into a block when you call puts on it. There is an  
example on ruby docs using json where they use:

require ‘json’  
puts {:hello =\> “goodbye”}.to\_json

This actually returns an error:

require ‘json’  
=\> true  
irb(main):013:0\> puts {:hello =\> “goodbye”}.to\_json  
SyntaxError: (irb):13: syntax error, unexpected =\>, expecting ‘}’  
puts {:hello =\> “goodbye”}.to\_json

Upon further investigation this wont even work:  
puts {:hello =\> “goodbye”}  
SyntaxError: (irb):10: syntax error, unexpected =\>, expecting ‘}’  
puts {:hello =\> “goodbye”}

yet this works:  
puts “hello” =\> “goodbye”  
{“hello”=\>“goodbye”}  
=\> nil

this is a hash:  
{ 1 =\> 1}.class  
=\> Hash

and now its a block:  
puts { 1 =\> 1}.class  
SyntaxError: (irb):8: syntax error, unexpected =\>, expecting ‘}’  
puts { 1 =\> 1}.class

I dont get how it goes from class hash to class block, its like magic

---

<div class="post-metadata">

**Author:** ![hpre](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/ac91a4/32.png) [@hpre](https://www.ruby-forum.com/u/hpre)\
**Post date:** [January 26, 2015, 9:57pm UTC](https://www.ruby-forum.com/t/hash-vs-block/233966/4 "2015-01-26T21:57:34Z")

</div>

Oh, okay, so that was the question.

I really don’t have a good answer for you, other than it being the  
tokenizer, which interprets the { as starting a block instead of  
starting a hash.

It works with

irb(main):009:0\> puts :hello =\> “world”  
{:hello=\>“world”}  
=\> nil

because the interpreter sees it as a hash, and passes that to the method  
`puts`.

In the case of

irb(main):010:0\> puts { :hello =\> “world” }  
SyntaxError: (irb):10: syntax error, unexpected =\>, expecting ‘}’  
puts { :hello =\> “world” }  
^  
from /Users/ohm/.rbenv/versions/2.2.0/bin/irb:11:in `’

The interpreter sees the { and thinks it’s the beginning of a block  
instead, trying to do something meaningful with it and failing.

If you want the original to work, you can wrap it in parentheses:

irb(main):011:0\> puts({:hello =\> “world”})  
{:hello=\>“world”}  
=\> nil
