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.
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
Assign the variable within the binding
# 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
Hash of class variables (with ’@@’)
# File lib/bondage.rb, line 24 def classvars; list_all :class_variables; end
Hash of constants
# File lib/bondage.rb, line 26 def consts; list_all :constants; end
Iterator over locals; base for Enumerable methods.
# File lib/bondage.rb, line 29 def each(&proc) locals.each(&proc) end
Hash of global variables (with ’$’)
# File lib/bondage.rb, line 20 def globals; list_all :global_variables; end
Hash of instance variables (with ’@’)
# File lib/bondage.rb, line 22 def instvars; list_all :instance_variables; end
Hash of local variables
# File lib/bondage.rb, line 18 def locals; list_all :local_variables; end
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
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.