3

Symbol keys in a hash seem to be handled similarly as with keyword arguments in the sense that, given hashes with symbol keys,

a = [{a: 1, b: 2}, {a: 3, b: 4}]

the corresponding values are referred to by the keyword block argument in this expression:

a.each{|a:, b:| ...}

On the other hand, this expression, in which the keyword argument appears within parentheses:

a.each_with_index{|(a:, b:), i| ...}

causes a syntax error:

unexpected tLABEL
a.each_with_index{|(a:, b:), i| }
                      ^

Is this an expected feature, or a bug?

sawa
  • 165,429
  • 45
  • 277
  • 381
  • 1
    Isn't this just a consequence of ruby's lexer? `def foo(a, b:, c:); end` works, whereas `def foo(a, (b:, c:)); end` give the same error as you encountered? I'm not sure why you'd ever want to use keyword arguments in a block like that, anyway. – Tom Lord Jun 28 '17 at 10:27
  • 1
    It’s ruby parser: `(0...a.size).zip(a).each{|i, a:, b:| puts a}` `#⇒ 1 3`. This rule is also known as “ladies first.” – Aleksei Matiushkin Jun 28 '17 at 10:56
  • [Read Here](https://stackoverflow.com/a/20634180/1978251) for a great explanation as to how "keyword" arguments are handled from a parameter listing stand point – engineersmnky Jun 28 '17 at 16:05

0 Answers0