Odd error

irb(main):006:0> {}.merge {:a => 3}
SyntaxError: compile error
(irb):6: syntax error, unexpected tASSOC, expecting ‘}’
{}.merge {:a => 3}

???
{}.merge({:a => 3}) works, though. Is this a grammar problem?

On 9-Nov-07, at 6:13 PM, Roger P. wrote:

irb(main):006:0> {}.merge {:a => 3}
SyntaxError: compile error
(irb):6: syntax error, unexpected tASSOC, expecting ‘}’
{}.merge {:a => 3}

???
{}.merge({:a => 3}) works, though. Is this a grammar problem?

Ambiguity I guess.

It is taking the {:a => 3} as a block rather than as a hash argument
(merge is defined to allow that). The parenthesis resolves the
ambiguity.

Cheers,
Bob


Bob H. – tumblelog at
http://www.recursive.ca/so/
Recursive Design Inc. – weblog at
http://www.recursive.ca/hutch
http://www.recursive.ca/ – works on
http://www.raconteur.info/cms-for-static-content/home/

Hi,

On Sat, 2007-11-10 at 08:19 +0900, Bob H. wrote:

Ambiguity I guess.

It is taking the {:a => 3} as a block rather than as a hash argument
(merge is defined to allow that). The parenthesis resolves the
ambiguity.

Right. “=>” is the tASSOC that ruby’s talking about. Since it thought
you were having a block there, the only valid thing you could have
after :a would be an end of block (i.e. :a is your return value).

Cheers,
Bob

Arlen