4 ms·
I've suspected Ruby's optional parentheses helped with some parsing or code generation activities, but after your comment I still can't find enough specific exa
by euroclydon 15y ago
I've suspected Ruby's optional parentheses helped with some parsing or code generation activities, but after your comment I still can't find enough specific examples or general theory. Is there a stated design objective for this in Ruby or can you point me to a good article on optional parents and DSLs?
By the way, I'm pouring over the source for Nancy, a Sinatra inspired micro framework in C# and I see where extension methods and implicit conversion operators are helpful in a DSL.
- chc 15y agoIt's part of the language's overriding design philosophy of pervasive OO. As much of the language as possible is implemented in terms of objects and methods, including seemingly syntactic things like "private", "attr_accessor", the top-level scope and all member access to other objects. It's all objects and methods under the hood, but you shouldn't have to think about it in those terms. You're not supposed to be concerned with how the accessor for an object's name is implemented — you're just supposed to feel like you're accessing it. Other languages have been moving in this direction, too. Objective-C has dot notation, C# lets you define getters and setters, Python has the @property decorator, Scala has special "magic" methods and quirky syntax rules to make this work, etc. All of these serve the same purpose as Ruby's optional parentheses: To make simple method calls less inherently "heavy".
- tptacek 15y agoHis specific question has a more prosaic answer, I think; optional parens make DSLs easier by making Ruby's syntax more flexible, thus allowing it to stand in for more languages. For instance, a few years ago, a summer internship project was to build a native debugger for OS X in pure Ruby, and to help with that, I wrote a chunk of X86 assembly as a Ruby DSL: http://news.ycombinator.com/item?id=284238 http://news.ycombinator.com/item?id=284238
- chc 15y agoI don't think so, at least not so directly. IIRC, this idea of "DSLs" in Ruby didn't gain currency until relatively late in the language's history, a little while after Rails made it explode and Why wrote his Poignant Guide featuring Dwemthy's Array. But Ruby had basically the same syntax long before that. I think the real answer is what I said: It makes Ruby's syntax lighter and more flexible so the pony could get more mileage out of its one trick (pure OO), and the convenience of writing DSLs just falls out of that. BTW, that DSL is an awesome hack.