# \<Class info\> from a String

**URL:** <https://www.ruby-forum.com/t/class-info-from-a-string/197463>\
**Category:** Ruby\
**Created:** [October 14, 2010, 8:03pm UTC](https://www.ruby-forum.com/t/class-info-from-a-string/197463 "2010-10-14T20:03:19Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![okkezSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/85f322/32.png) [@okkezSS](https://www.ruby-forum.com/u/okkezSS)\
**Post date:** [October 14, 2010, 8:03pm UTC](https://www.ruby-forum.com/t/class-info-from-a-string/197463/1 "2010-10-14T20:03:19Z")

</div>

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.  
😕

---

<div class="post-metadata">

**Author:** ![imp1](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/22d042/32.png) [@imp1](https://www.ruby-forum.com/u/imp1)\
**Post date:** [October 14, 2010, 8:28pm UTC](https://www.ruby-forum.com/t/class-info-from-a-string/197463/2 "2010-10-14T20:28:34Z")

</div>

On Thu, Oct 14, 2010 at 1:03 PM, Huw Taylor  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)wrote:

> a = Hew.new(“Broown Cew”)
> 
> –  
> Posted via [http://www.ruby-forum.com/](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"\>”

---

<div class="post-metadata">

**Author:** ![imp1](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/22d042/32.png) [@imp1](https://www.ruby-forum.com/u/imp1)\
**Post date:** [October 14, 2010, 9:08pm UTC](https://www.ruby-forum.com/t/class-info-from-a-string/197463/3 "2010-10-14T21:08:45Z")

</div>

On Thu, Oct 14, 2010 at 9:27 PM, Josh C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

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

Or, perhaps, marshal ([module Marshal - RDoc Documentation](http://ruby-doc.org/core/classes/Marshal.html))

Regards,  
Ammar

---

<div class="post-metadata">

**Author:** ![imp1](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/22d042/32.png) [@imp1](https://www.ruby-forum.com/u/imp1)\
**Post date:** [October 14, 2010, 9:16pm UTC](https://www.ruby-forum.com/t/class-info-from-a-string/197463/4 "2010-10-14T21:16:32Z")

</div>

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”)  
> > =\> #\<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

---

<div class="post-metadata">

**Author:** ![imp1](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/22d042/32.png) [@imp1](https://www.ruby-forum.com/u/imp1)\
**Post date:** [October 14, 2010, 11:46pm UTC](https://www.ruby-forum.com/t/class-info-from-a-string/197463/5 "2010-10-14T23:46:23Z")

</div>

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.  
> 😕

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”\>
