Sha256: 99179e00520f29209b9df551e7a05c0fb1108ae0c0ba26b9037da34dec409da5

Contents?: true

Size: 1.42 KB

Versions: 10

Compression:

Stored size: 1.42 KB

Contents

# TITLE:
#
#   Binding Variables
#
# SUMMARY:
#
#   Access binding variables. This requires the #eval.

require 'facets/binding/eval.rb'

#
class Binding

  def self.included(base) #:nodoc:
    base.send(:include, Eval)
  end

  # Returns the local variables defined in the binding context
  #
  #   a = 2
  #   binding.local_variables  #=> ["a"]
  #
  def local_variables()
    eval("local_variables")
  end

  # Returns the value of some variable.
  #
  #   a = 2
  #   binding["a"]  #=> 2
  #
  def []( x )
    eval( x.to_s )
  end

  # Set the value of a local variable.
  #
  #   binding["a"] = 4
  #   a  #=> 4
  #
  def []=( l, v )
    eval( "lambda {|v| #{l} = v}").call( v )
  end

end



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

  require 'test/unit'

  class TestBindingVariables < Test::Unit::TestCase

    def setup
      a = 1
      b = 2
      x = "hello"
      # the line number must be updated if it moves
      @bind = binding; @this_line_no = __LINE__
      @this_file_name = File.basename( __FILE__ ) # why does it equal basename only?
    end

    def test_local_variables
      assert_equal( ["a","b","x"], @bind.local_variables )
    end

    def test_op_store
      assert_nothing_raised{ @bind["x"] = "goodbye" }
      assert_equal( "goodbye", @bind["x"] )
    end

    def test_op_fetch
      assert_equal( "hello", @bind["x"] )
    end

  end

=end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
facets-2.0.0 lib/core/facets/binding/vars.rb
facets-2.0.1 lib/core/facets/binding/vars.rb
facets-2.0.2 lib/core/facets/binding/vars.rb
facets-2.0.5 lib/core/facets/binding/vars.rb
facets-2.1.1 lib/core/facets/binding/vars.rb
facets-2.1.2 lib/core/facets/binding/vars.rb
facets-2.1.0 lib/core/facets/binding/vars.rb
facets-2.0.3 lib/core/facets/binding/vars.rb
facets-2.0.4 lib/core/facets/binding/vars.rb
facets-2.1.3 lib/core/facets/binding/vars.rb