3

I recently upgraded to Ruby 2.1.3 and to my surprise I started getting some syntax errors. The smallest instance of the problem can be seen here:

{blah: if true then :bleh end}

which in Ruby 2.1.2 produces:

 => {:blah=>:bleh}

while in 2.1.3 produces:

SyntaxError: (irb):1: syntax error, unexpected modifier_if
{blah: if true then :bleh end}
         ^

A more realistic example would be:

{blah: bleh
 blih: if false
         blah
       elsif true
         bloh
       else
         bluh
       end}

(yes, it's not very common to write code like that, I know, but I got used to that in Haskell and I think it makes for very concise and readable code).

Did Ruby 2.1.3 break backward compatibility here? If so, this should be a bug according to the rules of semantic versioning, right?

Or was unknowingly I abusing a bug of the parser that got patched?

Is there some (other) way of writing if-conditions as expressions?

Pablo Fernandez
  • 279,434
  • 135
  • 377
  • 622

1 Answers1

3

the bug has been reported on ruby forums. Keep up to date with this link :

https://bugs.ruby-lang.org/issues/10279

as we can see :

just after a label, new expression should start, cannot be a modifier

The correct way of doing it in ruby 2.1.3 should be :

2.1.3 :006 > {blah: (if true then :bleh end)}
 => {:blah=>:bleh}
aelor
  • 10,892
  • 3
  • 32
  • 48