help for code understanding

L

Li Chen

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
 
S

Sebastian Hungerecker

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
 
J

James Gray

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)
=> # said:
o.show_self_and_self_dot_class
self: #<MyObject:0x576fc @arg=:arg>
self.class: MyObject
=> nil=> #<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 Gray II
 
L

Li Chen

James said:
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
 
J

James Gray

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?

They are not the same. Have a look:
Initializing 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 Gray II
 
L

Li Chen

Mike said:
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
 
L

Li Chen

James said:
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
 

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
473,733
Messages
2,569,440
Members
44,830
Latest member
ZADIva7383

Latest Threads

Top