From ae0e1a0682c853d0cdc969a82cd23455b4d2ca3e Mon Sep 17 00:00:00 2001 From: Rick Olson Date: Sat, 17 Jun 2006 03:01:57 +0000 Subject: Enhance Symbol#to_proc so it works with list objects, such as multi-dimensional arrays. Closes #5295 [nov@yo.rim.or.jp] git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@4455 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- activesupport/CHANGELOG | 5 +++++ activesupport/lib/active_support/core_ext/symbol.rb | 2 +- activesupport/test/core_ext/symbol_test.rb | 2 ++ 3 files changed, 8 insertions(+), 1 deletion(-) diff --git a/activesupport/CHANGELOG b/activesupport/CHANGELOG index 5a5fc9d98e..0c3598b351 100644 --- a/activesupport/CHANGELOG +++ b/activesupport/CHANGELOG @@ -1,5 +1,10 @@ *SVN* +* Enhance Symbol#to_proc so it works with list objects, such as multi-dimensional arrays. Closes #5295 [nov@yo.rim.or.jp]. Example: + + {1 => "one", 2 => "two", 3 => "three"}.sort_by(&:first).map(&:last) + #=> ["one", "two", "three"] + * Added Hash.create_from_xml(string) which will create a hash from a XML string and even typecast if possible [DHH]. Example: Hash.create_from_xml <<-EOT diff --git a/activesupport/lib/active_support/core_ext/symbol.rb b/activesupport/lib/active_support/core_ext/symbol.rb index 3a412f647b..a3dc794db4 100644 --- a/activesupport/lib/active_support/core_ext/symbol.rb +++ b/activesupport/lib/active_support/core_ext/symbol.rb @@ -7,6 +7,6 @@ class Symbol # # The same as people.select { |p| p.manager? }.collect { |p| p.salary } # people.select(&:manager?).collect(&:salary) def to_proc - Proc.new { |obj, *args| obj.send(self, *args) } + Proc.new { |*args| args.shift.__send__(self, *args) } end end diff --git a/activesupport/test/core_ext/symbol_test.rb b/activesupport/test/core_ext/symbol_test.rb index fec504d9d1..b30bd0b985 100644 --- a/activesupport/test/core_ext/symbol_test.rb +++ b/activesupport/test/core_ext/symbol_test.rb @@ -4,5 +4,7 @@ require File.dirname(__FILE__) + '/../../lib/active_support/core_ext/symbol' class SymbolTests < Test::Unit::TestCase def test_to_proc assert_equal %w(one two three), [:one, :two, :three].map(&:to_s) + assert_equal(%w(one two three), + {1 => "one", 2 => "two", 3 => "three"}.sort_by(&:first).map(&:last)) end end -- cgit v1.2.3