Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #8818: sym_to_proc_performance_improvement.patch

File sym_to_proc_performance_improvement.patch, 0.8 kB (added by lifofifo, 2 years ago)

Improve enumerator(&:sym) performance

  • activesupport/lib/active_support/core_ext/symbol.rb

    old new  
    11class Symbol 
    2   # Turns the symbol into a simple proc, which is especially useful for enumerations. Examples: 
     2  # Turns the symbol into a simple lambda, which is especially useful for enumerations. Examples: 
    33  # 
    44  #   # The same as people.collect { |p| p.name } 
    55  #   people.collect(&:name) 
     
    77  #   # The same as people.select { |p| p.manager? }.collect { |p| p.salary } 
    88  #   people.select(&:manager?).collect(&:salary) 
    99  def to_proc 
    10     Proc.new { |*args| args.shift.__send__(self, *args)
     10    lambda { |obj| obj.send self
    1111  end 
    1212end