/vendor/gems/facets-2.4.5/lib/core/facets/class/to_proc.rb
Ruby | 42 lines | 5 code | 7 blank | 30 comment | 0 complexity | 911c9f75d7e0e53bfca1c5f0f47813df MD5 | raw file
Possible License(s): GPL-3.0, GPL-2.0, IPL-1.0, AGPL-1.0, LGPL-3.0
- class Class
- # Convert instatiation of a class into a Proc.
- #
- # class Person
- # def initialize(name)
- # @name = name
- # end
- #
- # def inspect
- # @name.to_str
- # end
- # end
- #
- # %w(john bob jane hans).map(&Person) => [john, bob, jane, hans]
- #
- # CREDIT: Daniel Schierbeck
- def to_proc
- proc{|*args| new(*args)}
- end
- end
- =begin test
- reqiure 'test/unit'
- class TestClassConversion < Test::Unit::TestCase
- Person = Struct.new(:name)
- def test_to_proc
- people = ["joe"].map(&Person)
- assert_equal("joe", people[0].name)
- end
- end
- =end