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
···
--
Posted via http://www.ruby-forum.com/.
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
--
Posted via http://www.ruby-forum.com/.
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
Am Samstag 11 Juli 2009 17:16:10 schrieb Li Chen:
def seq
self.class.new(self)
endWhat does the code do, especially for class.new()?
Hi all,
I find a method in a source code as follows:
def seq
self.class.new(self)
endWhat 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 Gray II
On Jul 11, 2009, at 10:16 AM, Li Chen wrote:
James Gray 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
--
Posted via http://www.ruby-forum.com/\.
James Gray 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
enddef make_another_object2
self.clone
endAre 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 Gray II
On Jul 11, 2009, at 11:07 AM, Li Chen wrote:
Do you really need to use this sort of thing in 99% of practical
projects?
--
Posted via http://www.ruby-forum.com/.
James Gray 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
--
Posted via http://www.ruby-forum.com/\.
Mike Stephens 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
--
Posted via http://www.ruby-forum.com/\.