诗歌rails之 Symbol to Proc

  1. class Symbol
  2. # Turns the symbol into a simple proc, which is especially useful for enumerations. Examples:
  3. #
  4. # # The same as people.collect { |p| p.name }
  5. # people.collect(&:name)
  6. #
  7. # # The same as people.select { |p| p.manager? }.collect { |p| p.salary }
  8. # people.select(&:manager?).collect(&:salary)
  9. def to_proc
  10. Proc.new { |*args| args.shift.__send__(self, *args) }
  11. end
  12. end
posted @ 2009-07-07 13:37  麦飞  阅读(232)  评论(0编辑  收藏  举报