Cool Erlang like feature in Ruby
So, in joining the bandwagon, I’ve been reading about Erlang recently.
One of the features I really like is matchers:
1
This code will respond none for an argument array of no elements, 1 for 1 and so on. The two parts of this I like are a) the fact that Erlang matches against the number of variables, without extra syntax, and b) the fact that the underscore variables are discarded.
I just found out that you can discard variables in a parallel assignment in Ruby too!!!
1 name, _, ext = “first.name.spec“.split(“.“) 2 name # => “first” 3 ext # => “spec” 4
That my friends, is awesome, and could only be made better by allowing me to splat the _ (*_) to allow it to catch an arbitrary number of variables.
Enjoy!