Testing use of binding: got spurious error message

Hi All,

The program below ends with eval(…) producing an error message, as
shown in the comments at the end of the code. How can I direct the
offending output to a “bit bucket”?

Thanks in Advance,
Richard

class MyClass
def initialize(n)
@number = n
end

def getBinding
	binding()
end

end

def show(a)
a.each do |m|
puts “\t” + m.inspect
end
end

obj1 = MyClass.new 19
bind1 = obj1.getBinding

obj2 = MyClass.new 84
bind2 = obj2.getBinding

puts "number for obj1 = " + eval(’@number’, bind1).to_s
puts "number for obj2 = " + eval(’@number’, bind2).to_s

puts "# of methods: " + eval(methods.length.to_s, bind1).to_s
puts "Methods: "
dummy = eval(show(methods), bind1)
puts “That’s all folks!”

=begin

Result:

number for obj1 = 19
number for obj2 = 84

of methods: 46

Methods:
[snip]
TestBinding.rb:28:in `eval’: cannot convert Array into String
(TypeError)
from TestBinding.rb:28
=end

On Dec 3, 2006, at 8:15 PM, Richard wrote:

def show(a)

puts "number for obj1 = " + eval(’@number’, bind1).to_s
puts "number for obj2 = " + eval(’@number’, bind2).to_s

puts "# of methods: " + eval(methods.length.to_s, bind1).to_s
puts "Methods: "

dummy = eval(show(methods), bind1)

show(obj1.methods) # <= insert this
show(eval(“methods”, bind1)) # <= or this

puts “That’s all folks!”

Regards, Morton

Hi Morton,

show(obj1.methods) # <= insert this
show(eval(“methods”, bind1)) # <= or this

That’s perfect. Thank you very much.

Incidentally, I trimmed the quotes and sorted the list: a very
satisfying result!

Best wishes,
Richard