Testing use of binding: got spurious error message

R

Richard

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
 
M

Morton Goldberg

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)
show(obj1.methods) # <= insert this
show(eval("methods", bind1)) # <= or this
puts "That's all folks!"

Regards, Morton
 
R

Richard

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,608
Members
45,244
Latest member
cryptotaxsoftware12

Latest Threads

Top