News for hackers. Stuff that matters.
Re:New Ruby 1.9 Features, Tips & Tricks by masklinn on Monday February 21, 2011 @02:48PM (#2246862)
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?
Re:New Ruby 1.9 Features, Tips & Tricks by philwelch on Monday February 21, 2011 @03:20PM (#2246982)
Ruby 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.

Re:New Ruby 1.9 Features, Tips & Tricks by silentbicycle on Monday February 21, 2011 @11:33PM (#2248751)
How do all of these DSLs interact in close proximity? Ruby seems to do DSLs via a lenient lexer, rather than an explicit "quoting" mechanism, which means there isn't a way to locally specify a context for its idioms.
Re:New Ruby 1.9 Features, Tips & Tricks by philwelch on Wednesday February 23, 2011 @11:41AM (#2254708)
It doesn't turn out to be a big problem since you don't generally mix idioms in the same file. In a Rails app, it's pretty easy to figure out which files are evaluated by which DSL. If you're using HAML, anything in app/views with a .haml extension is HAML. Anything under /spec is Rspec. Capfile, Gemfile, and Rakefile are self explanatory. routes.rb uses the Rails routing DSL.
Re:New Ruby 1.9 Features, Tips & Tricks by silentbicycle on Wednesday February 23, 2011 @02:41PM (#2255456)
All right. Thanks for the polite and detailed response.
Re:New Ruby 1.9 Features, Tips & Tricks by carbon8 on Monday February 21, 2011 @03:45PM (#2247092)
"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.

Re:New Ruby 1.9 Features, Tips & Tricks by oewolf on Monday February 21, 2011 @04:08PM (#2247185)
And it isn't abuse until it is used in a specific abusive way. Having the options available to you isn't abusing it.
Re:New Ruby 1.9 Features, Tips & Tricks by sigzero on Monday February 21, 2011 @06:33PM (#2247813)
Doesn't it "bloat" the language though?
Re:New Ruby 1.9 Features, Tips & Tricks by oewolf on Monday February 21, 2011 @11:15PM (#2248697)
That is subjective. You can feel it is bloated, but I would call it handy and expressive.
Re:New Ruby 1.9 Features, Tips & Tricks by philwelch on Monday February 21, 2011 @07:36PM (#2248033)
Conventions 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.
Re:New Ruby 1.9 Features, Tips & Tricks by carbon8 on Monday February 21, 2011 @03:12PM (#2246946)
Agreed. 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_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.

Re:New Ruby 1.9 Features, Tips & Tricks by farrel on Monday February 21, 2011 @03:24PM (#2246997)
Proc#=== 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 (===).
Re:New Ruby 1.9 Features, Tips & Tricks by petercooper on Monday February 21, 2011 @08:06PM (#2248126)
Since 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 }
Re:New Ruby 1.9 Features, Tips & Tricks by rue on Monday February 21, 2011 @05:00PM (#2247415)
Or, more simply:

    case $stdin.gets.chomp
    when lambda {|matchobj| matchobj.count("!") > 1 }
      puts "Quitcher yelling!"
    when "hi"
      puts "Hi yourself"
    end