From 3694227f24d0f220c0ce7ef6c7f51598e2715d2d Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Wed, 20 May 2009 17:10:10 -0700 Subject: Break out Symbol#to_proc as a future-ruby extension --- .../lib/active_support/core_ext/symbol/to_proc.rb | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 activesupport/lib/active_support/core_ext/symbol/to_proc.rb (limited to 'activesupport/lib/active_support/core_ext/symbol') diff --git a/activesupport/lib/active_support/core_ext/symbol/to_proc.rb b/activesupport/lib/active_support/core_ext/symbol/to_proc.rb new file mode 100644 index 0000000000..520369452b --- /dev/null +++ b/activesupport/lib/active_support/core_ext/symbol/to_proc.rb @@ -0,0 +1,14 @@ +class Symbol + # Turns the symbol into a simple proc, which is especially useful for enumerations. Examples: + # + # # The same as people.collect { |p| p.name } + # people.collect(&:name) + # + # # The same as people.select { |p| p.manager? }.collect { |p| p.salary } + # people.select(&:manager?).collect(&:salary) + # + # This is a builtin method in Ruby 1.8.7 and later. + def to_proc + Proc.new { |*args| args.shift.__send__(self, *args) } + end unless :to_proc.respond_to?(:to_proc) +end -- cgit v1.2.3