In Files

Included Modules

Class Index [+]

Quicksearch

Bondage

bondage

DESCRIPTION:

class Binding stinks, module Bondage stinks less. Bondage provides hashes of local, global, instance, etc. variables, and provides the Enumerable interface for locals directly. It also provides [] syntax to get/set binding variables directly.

FEATURES/PROBLEMS:

SYNOPSIS

  require 'lib/bondage'

  x = 1
  y = 2

  b = binding.extend(Bondage)

  # return variable hashes
  p b.locals # => {:x=>1, :y=>2, :b=>#<Binding:0xxxxxxx>}
  p b.globals[:$PROGRAM_NAME] # => "synopis.rb"

  #concerned about the environement?
  p b.globals # => {:#!=>nil, :$SAFE=>0, ...}

  # set and retrieve
  b[:z] = ["one", "two", "three"]
  p b[:$LOAD_PATH] # => ["./lib", ...]
  p b[:@pizza] # => nil

  # supports Enumerable over locals
  p b.find {|var, value| value == 1 } # => [:x, 1]

  # Or if you're feeling really kinky
  class Binding
    include Bondage
  end

Constants

VERSION
(Not documented)

Public Instance Methods

[](name) click to toggle source

Alias for lookup

[]=(name, value) click to toggle source

Assign the variable within the binding

  • If your implementation supports ObjectSpace, this is relatively safe. This preserves object identity. (Copy by reference.)
  • If not, the right-hand-side gets it’s string value eval’ed
# File lib/bondage.rb, line 49
  def []=(name, value)
    if defined?(ObjectSpace)
      eval("#{sanitize(name)} = ObjectSpace._id2ref(#{value.object_id})")
    else
      eval("#{sanitize(name)} = #{value}")
    end
  end
classvars() click to toggle source

Hash of class variables (with ’@@’)

# File lib/bondage.rb, line 24
  def classvars; list_all :class_variables; end
consts() click to toggle source

Hash of constants

# File lib/bondage.rb, line 26
  def consts; list_all :constants; end
each(&proc) click to toggle source

Iterator over locals; base for Enumerable methods.

# File lib/bondage.rb, line 29
  def each(&proc)
    locals.each(&proc)
  end
globals() click to toggle source

Hash of global variables (with ’$’)

# File lib/bondage.rb, line 20
  def globals; list_all :global_variables; end
instvars() click to toggle source

Hash of instance variables (with ’@’)

# File lib/bondage.rb, line 22
  def instvars; list_all :instance_variables; end
locals() click to toggle source

Hash of local variables

# File lib/bondage.rb, line 18
  def locals; list_all :local_variables; end
lookup(name) click to toggle source

Return the value of the symbol in the binding.

# File lib/bondage.rb, line 35
  def lookup(name)
    begin
      eval(sanitize(name));
    rescue NameError
      return nil
    end
  end
Also aliased as: []
to_s() click to toggle source

Adds “+Bondage” to default string

# File lib/bondage.rb, line 12
  def to_s
    c = self.class.to_s
    super.sub(c, c + "+Bondage")
  end

Private Instance Methods

list_all(kind) click to toggle source

(Not documented)

# File lib/bondage.rb, line 58
  def list_all(kind)
    eval(kind.to_s).inject({}) {|hash, var|
      hash[var.to_sym] = lookup(var)
      hash
    }
  end
sanitize(name) click to toggle source

(Not documented)

# File lib/bondage.rb, line 65
  def sanitize(name)
    name.to_s.gsub(/[^$@\w]/, '_')
  end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.