Skip to content

Instantly share code, notes, and snippets.

@deepak
Created December 15, 2010 06:13
Show Gist options
  • Select an option

  • Save deepak/741688 to your computer and use it in GitHub Desktop.

Select an option

Save deepak/741688 to your computer and use it in GitHub Desktop.
how to find the size of a binding - less is good - can i be selective
str = "hello"
#c=0
#p = Proc.new { str.size }
#p.binding.eval("ObjectSpace.each_object { c +=1 }")
def enclose_binding(*attr)
p = Proc.new { attr[0].size }
code = <<-CODE
class_ivars = self.class.instance_variables || []
ivars = self.instance_variables
puts "ivars: #{ivars.inspect} | #{ivars.class}" #why is ivars not refined
cvars = (self.class_variables || []) if self.respond_to?(:class_variables)
class_cvars = (self.class.class_variables || []) if self.class.respond_to?(:class_variables)
vars = class_ivars + ivars + cvars + class_cvars + global_variables + local_variable
puts vars
CODE
#does not work - i do not think the ObjectSpace is in the scope of the binding
# p.binding.eval("c=0;ObjectSpace.each_object { c +=1 }")
p.binding.eval(code)
end
enclose_binding(str)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment