Sha256: 8df6ab0ab9a8e94ddb31434b421869e6d7a46285c08c149047a386cd4917f86f

Contents?: true

Size: 874 Bytes

Versions: 6

Compression:

Stored size: 874 Bytes

Contents

#--
# CREDIT Mikael Brockman
#++

module Kernel

  # A Ruby-ized realization of the K combinator.
  #
  #   with Book.new do |book|
  #     book.title = "Imperium"
  #     book.author = "Ulick Varange"
  #   end
  #
  # Also aliased as #returning for readability.
  #
  #   def foo
  #     returning values = [] do
  #       values << 'bar'
  #       values << 'baz'
  #     end
  #   end
  #
  #   foo # => ['bar', 'baz']

  def with(obj=self)
    yield obj
    obj
  end

  alias :returning :with

end


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

  require 'test/unit'

  class TCKernel < Test::Unit::TestCase

    def test_returning
      foo = returning( values = [] ) do
        values << 'bar'
        values << 'baz'
      end
      assert_equal( ['bar', 'baz'], foo )
    end

  end

=end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
facets-1.8.51 lib/facets/core/kernel/with.rb
facets-1.8.0 lib/facets/core/kernel/with.rb
facets-1.8.20 lib/facets/core/kernel/with.rb
facets-1.8.49 lib/facets/core/kernel/with.rb
facets-1.8.8 lib/facets/core/kernel/with.rb
facets-1.8.54 lib/facets/core/kernel/with.rb