<Class info> from a String

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.
:confused:

On Thu, Oct 14, 2010 at 1:03 PM, Huw Taylor
[email protected]wrote:

a = Hew.new(“Broown Cew”)


Posted via http://www.ruby-forum.com/.

If I understand what you mean, then what you really want is the inspect
method
b = a.inspect
=> “#<Hew:0x0000010086c440 @string=“Broown Cew”>”

On Thu, Oct 14, 2010 at 9:27 PM, Josh C. [email protected]
wrote:

end
I see that changing it to a string loses the @string = “Broown Cew” bit.

Or, perhaps, marshal (http://ruby-doc.org/core/classes/Marshal.html)

Regards,
Ammar

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 :frowning:

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”)
=> #<Hew:0x7f7b6ea152e8 @string=“Broown Cew”>

a.object_id.to_s(16)
=> “3fbdb750a974”

(a.object_id << 1).to_s(16)
=> “7f7b6ea152e8”

b = ObjectSpace._id2ref(Integer(“0x7f7b6ea152e8”) >> 1)
=> #<Hew:0x7f7b6ea152e8 @string=“Broown Cew”>

a.eql?(b)
=> true

On Oct 14, 2010, at 11:03 , Huw Taylor wrote:

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.
:confused:

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 = string
end

def to_s
“Hew new #{@string}”
end
end

cew = Hew.new(“Broown Cew”)

p cew

=> #<Hew:0x1001560c0 @string=“Broown Cew”>

puts cew

=> 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 = cew.to_yaml
puts s

=>

— !ruby/object:Hew

string: Broown Cew

p YAML.load(s)

=> #<Hew:0x1004aa848 @string=“Broown Cew”>