4 ms·
Most of it seems pretty nice (although I can hardly believe Ruby 1.8 does not have named groups), but... tip "Ruby 1.9 supports 4 ways to call a proc!"
by masklinn 16y ago
Most of it seems pretty nice (although I can hardly believe Ruby 1.8 does not have named groups), but...
tip "Ruby 1.9 supports 4 ways to call a proc!"
f =->n {[:hello, n]}
p f[:ruby] # => [:hello, :ruby]
p f.call(:ruby) # => [:hello, :ruby]
p f.(:ruby) # => [:hello, :ruby]
p f === :ruby # => [:hello, :ruby]
wtf? Why are they writing that like it's a good thing?
- carbon8 16y agoAgreed. If there is anyone here who knows the justification for the latter two, please explain. Edit: found the explanation for the last one: http://www.aimred.com/news/developers/2008/08/14/unlocking_the_power_of_case_equality_proc/ http://www.aimred.com/news/developers/2008/08/14/unlocking_t... So, basically, it's just using it as a normal case equality operator and you generally shouldn't use it in place of a regular #call.
- farrel 16y agoProc#=== allows you to do this: def multiple_of( n ) lambda{ |x| x.modulo( n ).zero? } end case 10 when multiple_of( 3 ): puts "3!" when multiple_of( 4 ): puts "4!" when multiple_of( 2 ): puts "2!" end Basically you can now run code when checking case equality (===).
- petercooper 16y agoSince we're talking Ruby 1.9 here, that code won't even run :-( .. drop the colons and you're good though! :-) But if we're really talking Ruby 1.9, you could also go with: ->(x){ x % n == 0 }
- rue 16y agoOr, more simply: case $stdin.gets.chomp when lambda {|matchobj| matchobj.count("!") > 1 } puts "Quitcher yelling!" when "hi" puts "Hi yourself" end
- philwelch 16y agoRuby is a language that encourages developing internal DSL's for a lot of problems. Having different syntaxes for the same thing gives you more flexibility in defining DSL's. For instance, the "f[:ruby]" form lets you define DSL's that intermix procs and hashes. The "f.(:ruby)" form is probably the new standard shorthand, but the explicit "f.call(:ruby)" is clearly there for backwards compatibility. If you want a language where there's only one way to do it, I think there's a language with that exact motto.
- carbon8 16y ago"If you want a language where there's only one way to do it, I think there's a language with that exact motto." Be that as it may, in Ruby you should also follow conventions, write readable code, use the most obvious choice when possible and avoid being unnecessarily clever. Most of the core principles that Python promotes should also be heeded when writing Ruby code. Ruby gives you the flexibility to choose different options, but that doesn't mean that the flexibility should be abused.
- philwelch 16y agoConventions vary from DSL to DSL. The flexibility doesn't have to be used by the application programmer to be useful. Think of how gemfiles or Rspec would look without optional parentheses in method calls. Even though parens are usually the way to go, sometimes you want different conventions in your DSL.
- oewolf 16y agoAnd it isn't abuse until it is used in a specific abusive way. Having the options available to you isn't abusing it.
- sigzero 16y agoDoesn't it "bloat" the language though?
- 16y ago