From: James Gray Date: 2009-07-12T00:28:31+09:00 Subject: Re: help for code understanding 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) => # >> o.show_self_and_self_dot_class self: # self.class: MyObject => nil >> o.make_another_object => #> 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