3 ms·
I like the idiom of mixed named and not named parameters in Ruby. You don't really need names for implied, required parameters. Consider: protagonist.throw
by erikpukinskis 14y ago
I like the idiom of mixed named and not named parameters in Ruby. You don't really need names for implied, required parameters. Consider:
protagonist.throw(potato, :force => 3, :at => zombie)
In this case a call to :throw without an object to throw is nonsense. Throw is a transitive verb, and the parameter is implied. On a throw method, the parameter is obviously the thing being throw'ed.
On the other hand, the optional parameters are totally obscure, and need to be named.
Similarly:
justice.join_in_matrimony(jay, bob)
The two unnamed parameters here are the things being join_in_matrimony'ed.
In other words: unnamed parameters are for direct objects, named parameters are for adjectives and prepositions.
Although arguably the Rubyish thing to do with prepositions is chain them:
protagonist.throw(potato, force: 3).at(zombie)
- grey-area 14y agoyes I see your point, it just feels inconsistent somehow, as named parameters are nicely self-documenting, could be used to generate docs etc, but that's probably just a foolish consistency. In your example you could be cute and have something like: protagonist.throw(a: => potato, :at => zombie, :force => 3) which would be just as nice. But I'm sure there are places where this will be useful/necessary, in your chained example with single args it feels forced to have to name them.