# Help for code understanding

**URL:** <https://www.ruby-forum.com/t/help-for-code-understanding/171928>\
**Category:** Ruby\
**Created:** [July 11, 2009, 5:16pm UTC](https://www.ruby-forum.com/t/help-for-code-understanding/171928 "2009-07-11T17:16:08Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [July 11, 2009, 5:16pm UTC](https://www.ruby-forum.com/t/help-for-code-understanding/171928/1 "2009-07-11T17:16:08Z")

</div>

Hi all,

I find a method in a source code as follows:

def seq  
self.class.new(self)  
end

What does the code do, especially for class.new()?

Thank you,

Li

---

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [July 11, 2009, 5:27pm UTC](https://www.ruby-forum.com/t/help-for-code-understanding/171928/2 "2009-07-11T17:27:36Z")

</div>

Am Samstag 11 Juli 2009 17:16:10 schrieb Li Chen:

> def seq  
> self.class.new(self)  
> end
> 
> What does the code do, especially for class.new()?

It calls the new method on the class of self, which will, presumably,  
create  
an instance of self’s class. In other words: It will create a new object  
of  
the same class as the object you invoke the seq method on.

HTH,  
Sebastian

---

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [July 11, 2009, 5:29pm UTC](https://www.ruby-forum.com/t/help-for-code-understanding/171928/3 "2009-07-11T17:29:57Z")

</div>

On Jul 11, 2009, at 10:16 AM, Li Chen wrote:

> Hi all,
> 
> I find a method in a source code as follows:
> 
> def seq  
> self.class.new(self)  
> end
> 
> What does the code do, especially for class.new()?

Does this help explain things?

> > class MyObject  
> > def show\_self\_and\_self\_dot\_class  
> > puts “self: %p\nself.class: %p” % [self, self.class]  
> > end  
> > def initialize(arg)  
> > @arg = arg  
> > end  
> > def make\_another\_object  
> > self.class.new(self)  
> > end  
> > end  
> > =\> nil
> 
> > o = MyObject.new(:arg)  
> > =\> #\<MyObject:0x576fc @arg=:arg\>
> 
> > o.show\_self\_and\_self\_dot\_class  
> > self: #\<MyObject:0x576fc @arg=:arg\>  
> > self.class: MyObject  
> > =\> nil
> 
> > o.make\_another\_object  
> > =\> #\<MyObject:0x54bdc @arg=#\<MyObject:0x576fc @arg=:arg\>\>

In plain English, it makes a new object of the same type as the  
current object and passes the current object as the argument to the  
constructor of the new object.

Hope that helps.

James Edward G. II

---

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [July 11, 2009, 6:51pm UTC](https://www.ruby-forum.com/t/help-for-code-understanding/171928/4 "2009-07-11T18:51:47Z")

</div>

On Jul 11, 2009, at 11:07 AM, Li Chen wrote:

> self.class.new  
> end
> 
> def make\_another\_object2  
> self.clone  
> end
> 
> Are they the same if self.class.new takes no argument?

They are not the same. Have a look:

> > class Copy  
> > def initialize(arg = nil)  
> > puts “Initializing a Copy…”  
> > @arg = arg  
> > end  
> > def make\_a\_copy  
> > self.class.new  
> > end  
> > end  
> > =\> nil
> 
> > c = Copy.new(:original)  
> > Initializing a Copy…  
> > =\> #\<Copy:0x5c954 @arg=:original\>
> 
> > c.clone  
> > =\> #\<Copy:0x5b090 @arg=:original\>
> 
> > c.make\_a\_copy  
> > Initializing a Copy…  
> > =\> #\<Copy:0x59204 @arg=nil\>

Notice how clone() didn’t call initialize() and it did copy the  
instance variable. Calling the constructor could change things  
though, as I’ve shown here.

clone() also duplicates some of Ruby’s internal state, like whether or  
not an object is frozen?(). Calling new() would make a fresh new  
object though and thus lose such details.

Hope that helps.

James Edward G. II

---

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [July 11, 2009, 6:07pm UTC](https://www.ruby-forum.com/t/help-for-code-understanding/171928/5 "2009-07-11T18:07:03Z")

</div>

James G. wrote:

> In plain English, it makes a new object of the same type as the  
> current object and passes the current object as the argument to the  
> constructor of the new object.

Thank you, James. Now I understand the code. But what is difference  
between these two methods(follow your previous example codes):

def make\_another\_object1  
self.class.new  
end

def make\_another\_object2  
self.clone  
end

Are they the same if self.class.new takes no argument?

Li

---

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [July 12, 2009, 6:03am UTC](https://www.ruby-forum.com/t/help-for-code-understanding/171928/6 "2009-07-12T06:03:10Z")

</div>

Mike S. wrote:

> Do you really need to use this sort of thing in 99% of practical  
> projects?

I don’t know but I would like to know what it does and what it means.  
If I simply ignore it I don’t I can improve my skill/knowledge on Ruby.

Li

---

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [July 12, 2009, 6:04am UTC](https://www.ruby-forum.com/t/help-for-code-understanding/171928/7 "2009-07-12T06:04:52Z")

</div>

James G. wrote:

> Notice how clone() didn’t call initialize() and it did copy the  
> instance variable. Calling the constructor could change things  
> though, as I’ve shown here.
> 
> clone() also duplicates some of Ruby’s internal state, like whether or  
> not an object is frozen?(). Calling new() would make a fresh new  
> object though and thus lose such details.

Hi James,

Thank you so much for your time to explain it so details. Your are  
really an expert on Ruby(from my point of view).

Li

---

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [July 11, 2009, 11:49pm UTC](https://www.ruby-forum.com/t/help-for-code-understanding/171928/8 "2009-07-11T23:49:24Z")

</div>

Do you really need to use this sort of thing in 99% of practical  
projects?
