2009年3月26日星期四

My bookmarks from Diigo 03/26/2009

  • tags: ruby, hash

  • tags: ruby, rails, hash, syntax

    • vehicles.select { |k,v| v > 20 }
      => { :planes=>21, :cars=>36}
    • Behold, our original code now does something beautiful:
    • vehicles = { :cars => 36, :boats => 8, :trains => 12, :planes => 21 }

      vehicles.select { |k,v| v > 20 }
      => [[ :planes, 21], [ :cars, 36]]
    • a = [[:planes, 21], [:cars, 36]].flatten
      => [ :planes, 21, :cars, 36]
    • Make It Part of Ruby




      If you do this a lot, you could wrap this up in a method… but you can also choose to modify the Hash class directly:

    • class Hash
      alias :old_select :select

      def select(&block)
      a = old_select(&block)
      Hash[*a.flatten]
      end
      end

Posted from Diigo. The rest of my favorite links are here.

没有评论: