Sha256: cf2585f400e21378cd760d8b10483e0c87157fbbf01d1a5429bb1402d146df68

Contents?: true

Size: 823 Bytes

Versions: 12

Compression:

Stored size: 823 Bytes

Contents

#--
# Credit goes to Florian Gross (flgr).
#++
class Symbol

  # Turn a symbol into a proc calling the method to
  # which it refers.
  #
  #   up = :upcase.to_proc
  #   up.call("hello")  #=> HELLO
  #
  # More useful is the fact that this allows <tt>&</tt>
  # to be used to coerce Symbol into Proc. 
  #
  #   %w{foo bar qux}.map(&:upcase)   #=> ["FOO","BAR","QUX"]
  #   [1, 2, 3].inject(&:+)           #=> 6
  #
  def to_proc
    lambda { |obj, *args| obj.send(self, *args) }
  end

end



#  _____         _
# |_   _|__  ___| |_
#   | |/ _ \/ __| __|
#   | |  __/\__ \ |_
#   |_|\___||___/\__|
#
=begin test

  require 'test/unit'

  class TCSymbol < Test::Unit::TestCase

    def test_to_proc
      assert_instance_of(Proc, up = :upcase.to_proc )
      assert_equal( "HELLO", up.call("hello") )
    end

  end

=end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
facets-1.0.3 packages/core/lib/facet/symbol/to_proc.rb
facets-0.9.0 lib/nano/symbol/to_proc.rb
facets-1.0.0 lib/facet/symbol/to_proc.rb
facets-1.1.0 lib/facet/symbol/to_proc.rb
facets-1.2.0 lib/facets/core/symbol/to_proc.rb
facets-1.2.1 lib/facets/core/symbol/to_proc.rb
facets-1.3.0 lib/facets/core/symbol/to_proc.rb
facets-1.3.1 lib/facets/core/symbol/to_proc.rb
facets-1.3.2 lib/facets/core/symbol/to_proc.rb
facets-1.3.3 lib/facets/core/symbol/to_proc.rb
facets-1.4.0 lib/facets/core/symbol/to_proc.rb
facets-1.4.1 lib/facets/core/symbol/to_proc.rb