# Object to class

**URL:** <https://rubytalk.org/t/object-to-class/41283>\
**Category:** ruby-talk\
**Created:** [10 October 2007 06:24 UTC](https://rubytalk.org/t/object-to-class/41283 "2007-10-10T06:24:16Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Pokkai\_Dokkai](https://avatars.discourse-cdn.com/v4/letter/p/7feea3/32.png) [@Pokkai\_Dokkai](https://rubytalk.org/u/Pokkai_Dokkai)\
**Post date:** [10 October 2007 06:24 UTC](https://rubytalk.org/t/object-to-class/41283/1 "2007-10-10T06:24:16Z")

</div>

is it possible to convert from a ruby object to Ruby class...  
any idea.....

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Alex\_Gutteridge](https://avatars.discourse-cdn.com/v4/letter/a/ce7236/32.png) [@Alex\_Gutteridge](https://rubytalk.org/u/Alex_Gutteridge)\
**Post date:** [10 October 2007 07:39 UTC](https://rubytalk.org/t/object-to-class/41283/2 "2007-10-10T07:39:58Z")

</div>

I'm not sure what you mean, but every Object has a class method which gives you it's class.

irb(main):008:0\> 1.class  
=\> Fixnum  
irb(main):009:0\> 'foo'.class  
=\> String

Alex Gutteridge

Bioinformatics Center  
Kyoto University

> **···**
>
> On 10 Oct 2007, at 15:24, Pokkai Dokkai wrote:
> 
> > is it possible to convert from a ruby object to Ruby class...  
> > any idea.....  
> > -- Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Sylvain\_Joyeux](https://avatars.discourse-cdn.com/v4/letter/s/96bed5/32.png) [@Sylvain\_Joyeux](https://rubytalk.org/u/Sylvain_Joyeux)\
**Post date:** [10 October 2007 08:02 UTC](https://rubytalk.org/t/object-to-class/41283/3 "2007-10-10T08:02:43Z")

</div>

What do you exactly have in mind ? Ruby classes are already Ruby  
objects ... so if you need to manipulate classes as objects you already  
can

obj = Class.new

\<manipulate obj\>

instance = obj.new

Sylvain

> **···**
>
> On Wednesday 10 October 2007, Pokkai Dokkai wrote:
> 
> > is it possible to convert from a ruby object to Ruby class...  
> > any idea.....

---

<div class="post-metadata">

**Author:** ![Shai\_Rosenfeld1](https://avatars.discourse-cdn.com/v4/letter/s/977dab/32.png) [@Shai\_Rosenfeld1](https://rubytalk.org/u/Shai_Rosenfeld1)\
**Post date:** [10 October 2007 08:22 UTC](https://rubytalk.org/t/object-to-class/41283/4 "2007-10-10T08:22:05Z")

</div>

Pokkai Dokkai wrote:

> is it possible to convert from a ruby object to Ruby class...  
> any idea.....

do u mean this?

[http://www.ruby-doc.org/core/classes/Object.html#M000348](http://www.ruby-doc.org/core/classes/Object.html#M000348)

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Pokkai\_Dokkai](https://avatars.discourse-cdn.com/v4/letter/p/7feea3/32.png) [@Pokkai\_Dokkai](https://rubytalk.org/u/Pokkai_Dokkai)\
**Post date:** [10 October 2007 10:47 UTC](https://rubytalk.org/t/object-to-class/41283/5 "2007-10-10T10:47:09Z")

</div>

Sylvain Joyeux wrote:

> **···**
>
> > On Wednesday 10 October 2007, Pokkai Dokkai wrote:
> > 
> > > is it possible to convert from a ruby object to Ruby class...  
> > > any idea.....
> > 
> > What do you exactly have in mind ? Ruby classes are already Ruby  
> > objects ... so if you need to manipulate classes as objects you already  
> > can
> > 
> > obj = Class.new
> > 
> > \<manipulate obj\>
> > 
> > instance = obj.new
> > 
> > Sylvain
> 
> source code here  
> ---------------------  
> class Name  
> &nbsp;&nbsp;def initialize  
> &nbsp;&nbsp;end  
> &nbsp;&nbsp;&nbsp;.....  
> end  
> -----------------------
> 
> obj=Name.new
> 
> now my question is ,how to create above source code ?.
> 
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Ari\_Brown](https://avatars.discourse-cdn.com/v4/letter/a/48db29/32.png) [@Ari\_Brown](https://rubytalk.org/u/Ari_Brown)\
**Post date:** [10 October 2007 11:26 UTC](https://rubytalk.org/t/object-to-class/41283/6 "2007-10-10T11:26:31Z")

</div>

Maybe he means something like extracting Ruby code for the class out of the object... That would be very interesting, because you could really delve in and affect all sorts of bug fixes for compiled extensions.

AFAIK, this is not possible. Take a look at ruby2ruby

I'm guessing something like

a = YourClass.new  
a.to\_ruby

Ari  
--------------------------------------------|  
If you're not living on the edge,  
then you're just wasting space.

> **···**
>
> On Oct 10, 2007, at 4:22 AM, Shai Rosenfeld wrote:
> 
> > Pokkai Dokkai wrote:
> > 
> > > is it possible to convert from a ruby object to Ruby class...  
> > > any idea.....
> > 
> > do u mean this?
> > 
> > [class Object - RDoc Documentation](http://www.ruby-doc.org/core/classes/Object.html#M000348)

---

<div class="post-metadata">

**Author:** ![Pokkai\_Dokkai](https://avatars.discourse-cdn.com/v4/letter/p/7feea3/32.png) [@Pokkai\_Dokkai](https://rubytalk.org/u/Pokkai_Dokkai)\
**Post date:** [10 October 2007 10:50 UTC](https://rubytalk.org/t/object-to-class/41283/7 "2007-10-10T10:50:23Z")

</div>

source code here

> **···**
>
> ---------------------  
> class Name  
> &nbsp;&nbsp;def initialize  
> &nbsp;&nbsp;end  
> &nbsp;&nbsp;&nbsp;.....  
> end  
> -----------------------
> 
> obj=Name.new
> 
> now my question is ,how to create above source code from obj?.
> 
> actually jruby will create java object to ruby object  
> but that ruby object(from java object) is not in source code format
> 
> so i am asking this question ......  
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [10 October 2007 10:51 UTC](https://rubytalk.org/t/object-to-class/41283/8 "2007-10-10T10:51:52Z")

</div>

If you want to create the class definition source code when having an  
instance only there is no way - at least no standard way. You would  
have to dig into the Ruby interpreter to do that.

robert

> **···**
>
> 2007/10/10, Pokkai Dokkai \<bad\_good\_lion@yahoo.com\>:
> 
> > Sylvain Joyeux wrote:  
> > \> On Wednesday 10 October 2007, Pokkai Dokkai wrote:  
> > \>\> is it possible to convert from a ruby object to Ruby class...  
> > \>\> any idea.....  
> > \> What do you exactly have in mind ? Ruby classes are already Ruby  
> > \> objects ... so if you need to manipulate classes as objects you already  
> > \> can  
> > \>  
> > \> obj = Class.new  
> > \>  
> > \> \<manipulate obj\>  
> > \>  
> > \> instance = obj.new  
> > \>  
> > \> Sylvain
> > 
> > source code here  
> > ---------------------  
> > class Name  
> > &nbsp;&nbsp;def initialize  
> > &nbsp;&nbsp;end  
> > &nbsp;&nbsp;&nbsp;.....  
> > end  
> > -----------------------
> > 
> > obj=Name.new
> > 
> > now my question is ,how to create above source code ?.

---

<div class="post-metadata">

**Author:** ![\_Pena\_Botp1](https://avatars.discourse-cdn.com/v4/letter/_/94ad74/32.png) [@\_Pena\_Botp1](https://rubytalk.org/u/_Pena_Botp1)\
**Post date:** [11 October 2007 02:10 UTC](https://rubytalk.org/t/object-to-class/41283/9 "2007-10-11T02:10:03Z")

</div>

# AFAIK, this is not possible. Take a look at ruby2ruby

cool

# I'm guessing something like

> **···**
>
> From: Ari Brown [[mailto:ari@aribrown.com](mailto:ari@aribrown.com)]  
> #  
> # a = YourClass.new  
> # a.to\_ruby
> 
> arggh, i'm feeling dumb, i've just downloaded the gem, but i do not know how to require it. The rdoc does not give an example.
> 
> kind regards -botp

---

<div class="post-metadata">

**Author:** ![\_Pena\_Botp1](https://avatars.discourse-cdn.com/v4/letter/_/94ad74/32.png) [@\_Pena\_Botp1](https://rubytalk.org/u/_Pena_Botp1)\
**Post date:** [11 October 2007 02:24 UTC](https://rubytalk.org/t/object-to-class/41283/10 "2007-10-11T02:24:54Z")

</div>

# arggh, i'm feeling dumb, i've just downloaded the gem, but i  
# do not know how to require it. The rdoc does not give an example.

pls ignore. dumb indeed. forgot the 'rubygems' require. gotta change my irb ini.

my next question is, will this run on windows?  
if yes, examples pls.

thanks and kind regards -botp

> **···**
>
> From: Peña, Botp [[mailto:botp@delmonte-phil.com](mailto:botp@delmonte-phil.com)]

---

<div class="post-metadata">

**Author:** ![James\_Britt](https://avatars.discourse-cdn.com/v4/letter/j/22d042/32.png) [@James\_Britt](https://rubytalk.org/u/James_Britt)\
**Post date:** [12 October 2007 09:37 UTC](https://rubytalk.org/t/object-to-class/41283/11 "2007-10-12T09:37:48Z")

</div>

Well, it could be sort of possible, but partly pointless:  
You could start a ruby process that then loads a file, but before loading, read the file. You could then create objects that contain the code (as a string, probably) for classes in the original file.  
Then when you ask an object, "what class are you?", you will have a little clipboard (or PDA) to lookup his/her class name and fetch your class\_code string.

From there it would be possible to alter the class\_code string, then re"load" that class into the interpreter...  
circular? could be  
pointless? maybe  
fun? probably

> **···**
>
> On Oct 10, 2007, at 5:50 AM, Pokkai Dokkai wrote:
> 
> > source code here  
> > ---------------------  
> > class Name  
> > &nbsp;&nbsp;def initialize  
> > &nbsp;&nbsp;end  
> > &nbsp;&nbsp;&nbsp;.....  
> > end  
> > -----------------------
> > 
> > obj=Name.new
> > 
> > now my question is ,how to create above source code from obj?.
> > 
> > actually jruby will create java object to ruby object  
> > but that ruby object(from java object) is not in source code format
> > 
> > so i am asking this question ......  
> > -- Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).
