0

While trying to answer another member's question, I happened across this strange behaviour:

puts :some_object => 'another_object'

Surprisingly, the output is this:

{:some_object=>"another_object"}

What is this new devilry? It looks as though I've created a hash using #puts, and without using the normal curly-bracket syntax.

I can't test this theory though, because this just generates an error:

puts (:some_object => 'another_object').class

# => syntax error, unexpected =>, expecting ')'

What's going on here?

Edit: Okay, thanks to bundacia's explanation, it's now easy for me to test and confirm that it's a hash (whereas I wasn't sure how to do that before):

def test(x)
  puts x.class
end
test(:some_object => 'another_object')

# => Hash

Many thanks!

Kal
  • 2,098
  • 24
  • 23

1 Answers1

3

You are passing a hash to puts. In ruby, if the last argument you're passing to a function is a hash the curly braces are optional. So your example is equivalent to:

puts( {:some_object => 'another_object'} )
bundacia
  • 1,036
  • 5
  • 13
  • Thanks for that. I must say I find that strange, but with that explanation it's easy to confirm the class now. – Kal Jan 15 '14 at 17:24