help for code understanding

Discussion in 'Ruby' started by Li Chen, Jul 11, 2009.

  1. Li Chen

    Li Chen Guest

    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/.
     
    Li Chen, Jul 11, 2009
    #1
    1. Advertising

  2. 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
     
    Sebastian Hungerecker, Jul 11, 2009
    #2
    1. Advertising

  3. Li Chen

    James Gray Guest

    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 Gray II
     
    James Gray, Jul 11, 2009
    #3
  4. Li Chen

    Li Chen Guest

    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/.
     
    Li Chen, Jul 11, 2009
    #4
  5. Li Chen

    James Gray Guest

    On Jul 11, 2009, at 11:07 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?


    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=:eek:riginal>
    >> c.clone

    => #<Copy:0x5b090 @arg=:eek:riginal>
    >> 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
     
    James Gray, Jul 11, 2009
    #5
  6. Mike Stephens, Jul 11, 2009
    #6
  7. Li Chen

    Li Chen Guest

    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/.
     
    Li Chen, Jul 12, 2009
    #7
  8. Li Chen

    Li Chen Guest

    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/.
     
    Li Chen, Jul 12, 2009
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. JM
    Replies:
    3
    Views:
    2,184
    Andy Dingley
    Oct 1, 2005
  2. Dhruva Hein

    Help understanding code

    Dhruva Hein, Apr 10, 2005, in forum: Python
    Replies:
    2
    Views:
    345
    Bruno Desthuilliers
    Apr 11, 2005
  3. Fredrik Lundh

    Re: Help understanding code

    Fredrik Lundh, Apr 10, 2005, in forum: Python
    Replies:
    1
    Views:
    337
    Duncan Booth
    Apr 11, 2005
  4. Replies:
    2
    Views:
    424
    Phlip
    Mar 15, 2007
  5. Need help in understanding c code

    , Jun 1, 2007, in forum: C Programming
    Replies:
    2
    Views:
    324
    Barry Schwarz
    Jun 2, 2007
Loading...

Share This Page