PageRenderTime 100ms CodeModel.GetById 50ms app.highlight 2ms RepoModel.GetById 38ms app.codeStats 0ms

/vendor/gems/facets-2.4.5/lib/core/facets/class/to_proc.rb

https://bitbucket.org/mediashelf/fedora-migrator
Ruby | 42 lines | 5 code | 7 blank | 30 comment | 0 complexity | 911c9f75d7e0e53bfca1c5f0f47813df MD5 | raw file
 1class Class
 2
 3  # Convert instatiation of a class into a Proc.
 4  #
 5  #   class Person
 6  #     def initialize(name)
 7  #       @name = name
 8  #     end
 9  #
10  #     def inspect
11  #       @name.to_str
12  #     end
13  #   end
14  #
15  #   %w(john bob jane hans).map(&Person) => [john, bob, jane, hans]
16  #
17  # CREDIT: Daniel Schierbeck
18
19  def to_proc
20    proc{|*args| new(*args)}
21  end
22
23end
24
25
26
27=begin test
28  reqiure 'test/unit'
29
30  class TestClassConversion < Test::Unit::TestCase
31
32    Person = Struct.new(:name)
33
34    def test_to_proc
35      people = ["joe"].map(&Person)
36      assert_equal("joe", people[0].name)
37    end
38
39  end
40
41=end
42