Hacker News new | past | comments | ask | show | jobs | submit login

I am reminded of something in Brian Carper's old post "Keyword Arguments: Ruby, Clojure, Common Lisp":

http://briancarper.net/blog/579/keyword-arguments-ruby-cloju...

This reminds me of everything I love, but also everything I hate, about Ruby. He writes:

------

With even more added sugar, you can leave off the parens in Ruby function calls. So this is pretty common in Ruby:

foo :x => 123 # => {:x=>123}

How nice and punctuation-less. But then things get ugly. What about this?

foo {:x => 123}

That won't even compile.




Hah, I had to try this out to believe it.

You can do `foo d: {:y => 2}` or `foo :d => {:y => 2}` but not a raw hash literal without parens.

That's parse.y[1] for you...

[1]: https://github.com/ruby/ruby/blob/trunk/parse.y


It's because Ruby thinks {} is a block. All Ruby methods can implicitly take a block (why? who knows...), so the single-line block syntax is parsed as such before it can be considered a hash argument into the method.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: