Mimic Methods
Much of Ruby’s appeal comes from its flexible syntax. You can find an example of this flexiblity even in the most basic program:
| puts 'Hello, world' |
Newcomers to Ruby often mistake puts for a language keyword, when it’s actually a method. People usually leave out the parentheses when calling puts, so it doesn’t look like a method. Reinsert the parentheses, and the nature of puts becomes obvious:
| puts('Hello, world') |
Thanks to disguised method calls such as this one, Ruby manages to provide many useful function-like methods while keeping the core of the language relatively small and uncluttered.
This simple idea of dropping parentheses from method calls is used quite often by expert coders. Sometimes you’ll want to keep ...
Get Metaprogramming Ruby 2 now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.