Sha256: f68101b8ebd6edc82e265f725e70aaa80930ddc4a1eb8207462235190d6349ed

Contents?: true

Size: 623 Bytes

Versions: 5

Compression:

Stored size: 623 Bytes

Contents

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

Version data entries

5 entries across 5 versions & 2 rubygems

Version Path
facets-2.4.2 lib/core/facets/class/to_proc.rb
facets-2.4.3 lib/core/facets/class/to_proc.rb
facets-2.4.4 lib/core/facets/class/to_proc.rb
facets-2.4.5 lib/core/facets/class/to_proc.rb
mack-facets-0.8.2 lib/gems/facets-2.4.5/lib/core/facets/class/to_proc.rb