[Solved] Why {{}} is syntax error while [[]] isn’t? [closed]


You create a hash by providing zero or more key-value pairs.

  • {} creates a hash with a zero key-value pairs, i.e. an empty hash
  • {'foo' => 'bar'} creates a hash with a single pair (key 'foo' and value 'bar')
  • {'foo'} raises a SyntaxError because there’s only a key, the value is missing

The error message says the same: unexpected '}', expecting =>, i.e. Ruby expects a => after 'foo', not a closing }.

{{}} raises the same SyntaxError because the value is missing. The hash equivalent to [[]] would probably look like this:

{{}=>{}}

1

solved Why {{}} is syntax error while [[]] isn’t? [closed]