3 ms·
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-c
by lkrubner 11y ago
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-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.
- cronin101 11y agoHah, 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 https://github.com/ruby/ruby/blob/trunk/parse.y
- tomphoolery 11y agoIt'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.