4 ms·
ruby does have its share of cruft, but it also has some inspired features that make a far bigger difference than you'd think they would. blocks are the biggest
by zem 15y ago
ruby does have its share of cruft, but it also has some inspired features that make a far bigger difference than you'd think they would. blocks are the biggest (specifically, syntactic support for attaching a lexical closure to every method call, which the object can choose to use or not), but here's another cute one: the case statement
case object
when matcher1; ...
when matcher2; ...
end
performs a method call on the matchers, with the object as an argument, rather than vice versa. so you can say things like
case name
when "John Smith"; puts "hello again"
when / Smith$/; puts "are you related to john?"
end
the string "John Smith" and the regexp / Smith$/ will each call a different matcher to see if they match your name - a far cleaner design than the string class having to provide matchers for everything you might want to use in a case statement.