<Class info> from a String.

H

Huw Taylor

If I were to have this:

class Hew
def initialize(string)
@string = string
end
def other_method
end
end

a = Hew.new("Broown Cew")
=> #<Hew:0x40e8238 @string = "Broown Cew">

b = a.to_s
=> "#<Hew:0x40e8238>"

My question is: Is there anyway to get the class (and its info) from b
(i.e what is now a string)?

I see that changing it to a string loses the @string = "Broown Cew" bit.
:/
 
J

Josh Cheek

[Note: parts of this message were removed to make it a legal post.]

If I were to have this:

class Hew
def initialize(string)
@string = string
end
def other_method
end
end

a = Hew.new("Broown Cew")
=> #<Hew:0x40e8238 @string = "Broown Cew">

b = a.to_s
=> "#<Hew:0x40e8238>"

My question is: Is there anyway to get the class (and its info) from b
(i.e what is now a string)?

I see that changing it to a string loses the @string = "Broown Cew" bit.
:/
If I understand what you mean, then what you really want is the inspect
method
b = a.inspect
=> "#<Hew:0x0000010086c440 @string=\"Broown Cew\">"
 
B

Brian Candler

Huw Taylor wrote in post #950237:
If I were to have this:

class Hew
def initialize(string)
@string = string
end
def other_method
end
end

a = Hew.new("Broown Cew")
=> #<Hew:0x40e8238 @string = "Broown Cew">

b = a.to_s
=> "#<Hew:0x40e8238>"

My question is: Is there anyway to get the class (and its info) from b
(i.e what is now a string)?

Maybe you're looking for ObjectSpace._id2ref

Unfortunately, the hex pointer shown by #inspect doesn't always map
directly to the object's object_id, and I think the algorithm to map it
varies with the version of ruby you're running :-(

But here in principle is how you recover a reference to the object:
RUBY_DESCRIPTION => "ruby 1.8.7 (2010-01-10 patchlevel 249) [x86_64-linux]"
a = Hew.new("Broown Cew")
=> # said:
a.object_id.to_s(16) => "3fbdb750a974"
(a.object_id << 1).to_s(16) => "7f7b6ea152e8"
b = ObjectSpace._id2ref(Integer("0x7f7b6ea152e8") >> 1)
=> # said:
a.eql?(b)
=> true
 
R

Ryan Davis

If I were to have this:
=20
class Hew
def initialize(string)
@string =3D string
end
def other_method
end
end
=20
a =3D Hew.new("Broown Cew")
=3D> #<Hew:0x40e8238 @string =3D "Broown Cew">
=20
b =3D a.to_s
=3D> "#<Hew:0x40e8238>"
=20
My question is: Is there anyway to get the class (and its info) from b
(i.e what is now a string)?
=20
I see that changing it to a string loses the @string =3D "Broown Cew" = bit.
:/

There are two potential questions hidden in this problem:

Q1) How do I make the string representation of my object more readable?
A1) Define your own #to_s:

class Hew
def initialize(string)
@string =3D string
end

def to_s
"Hew new #{@string}"
end
end

cew =3D Hew.new("Broown Cew")

p cew
# =3D> #<Hew:0x1001560c0 @string=3D"Broown Cew">
puts cew
# =3D> Hew new Broown Cew

Q2) How do I get an object back from a string representation?
A2) Don't use to_s to get a string representation, use YAML, JSON, =
Marshal, or something else equally suited to serialize and deserialize =
your object into a readable presentation:

require 'yaml'
s =3D cew.to_yaml
puts s
# =3D>
# --- !ruby/object:Hew
# string: Broown Cew
p YAML.load(s)
# =3D> #<Hew:0x1004aa848 @string=3D"Broown Cew">
 

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
474,143
Messages
2,570,822
Members
47,368
Latest member
michaelsmithh

Latest Threads

Top