DISQUS

R-oooh!-by: A Shorter `collect`

  • Hendrik Mans · 9 months ago
    AFAIK this is something ActiveSupport introduces, so it won't work outside of Rails. The good news is that this will work with other methods taking blocks as well, like group_by. HAEV FUNS!
  • Carlo Zottmann · 9 months ago
    Apparently not: even without ActiveSupport this will work. (Using OpenStruct to simulate "real" attributes here.)

    require "ostruct"
    [ OpenStruct.new({:a => 1, :b => 2}), OpenStruct.new({ :a => 3, :b => 5}) ].collect(&:a)
  • Mr Matt · 9 months ago
    Help conserve characters even further by using map:

    Customers.find(:all).map(&:email)
  • Carlo Zottmann · 9 months ago
    True. And even that can be shortened:

    Customers.all.map(&:email)

    Hehe. :)