actual class name

P

Perry Smith

I'm trying to debug a problem I have with YAML. When I do:

puts "#{foo.class}"

I get (for example) Xyz

but I know that foo is not really of class Xyz. I think it is an
YAML::Object. The reason this happens is because to_s has been
overridden.

How can I get the real name of the class of an object?

I think this is what YAML::Object is doing:

#! /usr/bin/env ruby

class Xyz
def self.to_s
"dog"
end
end

x = Xyz.new
puts "#{x.class}"

I get "dog" instead of Xyz. How can I get Xyz in this case?
 
R

Ryan Davis

I'm trying to debug a problem I have with YAML. When I do:

Why not tell us about the real problem and not the symptom?
puts "#{foo.class}"
=20
I get (for example) Xyz
=20
but I know that foo is not really of class Xyz. I think it is an
YAML::Object. The reason this happens is because to_s has been
overridden.

Are you sure?
How can I get the real name of the class of an object?
=20
I think this is what YAML::Object is doing:
=20
#! /usr/bin/env ruby
=20
class Xyz
def self.to_s
"dog"
end
end
=20
x =3D Xyz.new
puts "#{x.class}"
=20
I get "dog" instead of Xyz. How can I get Xyz in this case?

You can try x.class.name instead... Or you can try to restore the real =
name with:

x.class.send :remove_method, :to_s
 
P

Perry Smith

Thanks.

The commented out line works -- I get back an AsyncRequest instance that
is able to call perform. The ar = job.ybody does not work. I get back
a YAML::Object. (The debug line is hit)

# ar = YAML.load(job.body) # The transmitted AsyncRequest
ar = job.ybody # The transmitted AsyncRequest
if ar.kind_of? YAML::Object
Rails.logger.debug("YAML::Object")
end
Rails.logger.debug("self.perform ar.class = #{ar.class.to_s}")
ar.perform

I'm interfacing with beanstalk-client and ybody is defined as:

def ybody()
(@ybody ||= [begin YAML.load(body) rescue nil end])[0]
end

I'm trying to figure out why my call to load works but the call inside
the gem is not working.
 

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

Forum statistics

Threads
473,968
Messages
2,570,153
Members
46,701
Latest member
XavierQ83

Latest Threads

Top