Adding a new method to a class

Discussion in 'Ruby' started by Mickael Faivre-Macon, Sep 24, 2006.

  1. Hi,

    What is called the process of adding a new method of an already defined class ?

    For example:

    class A
    def a
    end
    end

    And in another file:

    class A
    def b
    end
    end

    And now my real question:

    What if I want to declare a new class variable in the initialize method ?
    If it's a derived class I can call super:
    def initialize
    super
    @my_var = ""
    end

    but if it not derived ?
    How can I do this ?

    Thanks,
    Mickael
     
    Mickael Faivre-Macon, Sep 24, 2006
    #1
    1. Advertising

  2. Mickael Faivre-Macon

    MonkeeSage Guest

    Mickael Faivre-Macon wrote:
    > What is called the process of adding a new method of an already defined class ?


    Hi Mickael,

    I'm not sure of any official technical name but I often see "extend"
    used for a previously undefined method and "override" for redefining an
    existing method.

    > What if I want to declare a new class variable in the initialize method ?
    > If it's a derived class I can call super:
    > def initialize
    > super
    > @my_var = ""
    > end
    >
    > but if it not derived ?
    > How can I do this ?


    Well a @var is an instance variable, a class variable has two @, like
    @@var. but in either case, you would just add it (in ruby you just
    assign to a variable and it comes into existence, not need to "declare"
    them any special way).

    class Tree
    def initialize
    @cat = 'in it'
    @@dog = 'bark'
    end
    end

    Regards,
    Jordan
     
    MonkeeSage, Sep 24, 2006
    #2
    1. Advertising

  3. Mickael Faivre-Macon wrote:
    > Hi,
    >
    > What is called the process of adding a new method of an already defined
    > class ?
    >
    > For example:
    >
    > class A
    > def a
    > end
    > end
    >
    > And in another file:
    >
    > class A
    > def b
    > end
    > end
    >
    > And now my real question:
    >
    > What if I want to declare a new class variable in the initialize method ?
    > If it's a derived class I can call super:
    > def initialize
    > super
    > @my_var = ""
    > end
    >
    > but if it not derived ?
    > How can I do this ?


    You can alias the old initialize method and replace it with another one:

    irb(main):014:0> class A
    irb(main):015:1> def initialize(x)
    irb(main):016:2> @foo = x
    irb(main):017:2> end
    irb(main):018:1> end
    => nil
    irb(main):019:0> a = A.new 10
    => #<A:0x38e4d0 @foo=10>
    irb(main):020:0> class A
    irb(main):021:1> alias initialize_old initialize
    irb(main):022:1> def initialize(x)
    irb(main):023:2> initialize_old x
    irb(main):024:2> @bar = "foo"
    irb(main):025:2> end
    irb(main):026:1> end
    => nil
    irb(main):027:0> b = A.new 20
    => #<A:0x374610 @bar="foo", @foo=20>

    Kind regards

    robert
     
    Robert Klemme, Sep 24, 2006
    #3
  4. Thank you MonkeeSage for your precisions.
    Thanks Robert, that's what I was looking for.

    Mickael.

    On 9/24/06, Robert Klemme <> wrote:
    > Mickael Faivre-Macon wrote:
    > > Hi,
    > >
    > > What is called the process of adding a new method of an already defined
    > > class ?
    > >
    > > For example:
    > >
    > > class A
    > > def a
    > > end
    > > end
    > >
    > > And in another file:
    > >
    > > class A
    > > def b
    > > end
    > > end
    > >
    > > And now my real question:
    > >
    > > What if I want to declare a new class variable in the initialize method ?
    > > If it's a derived class I can call super:
    > > def initialize
    > > super
    > > @my_var = ""
    > > end
    > >
    > > but if it not derived ?
    > > How can I do this ?

    >
    > You can alias the old initialize method and replace it with another one:
    >
    > irb(main):014:0> class A
    > irb(main):015:1> def initialize(x)
    > irb(main):016:2> @foo = x
    > irb(main):017:2> end
    > irb(main):018:1> end
    > => nil
    > irb(main):019:0> a = A.new 10
    > => #<A:0x38e4d0 @foo=10>
    > irb(main):020:0> class A
    > irb(main):021:1> alias initialize_old initialize
    > irb(main):022:1> def initialize(x)
    > irb(main):023:2> initialize_old x
    > irb(main):024:2> @bar = "foo"
    > irb(main):025:2> end
    > irb(main):026:1> end
    > => nil
    > irb(main):027:0> b = A.new 20
    > => #<A:0x374610 @bar="foo", @foo=20>
    >
    > Kind regards
    >
    > robert
     
    Mickael Faivre-Macon, Sep 24, 2006
    #4
    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. E11
    Replies:
    1
    Views:
    4,939
    Thomas Weidenfeller
    Oct 12, 2005
  2. Pawel_Iks
    Replies:
    3
    Views:
    553
    terminator
    Jul 31, 2007
  3. marekw2143
    Replies:
    3
    Views:
    1,397
    marekw2143
    Jul 25, 2009
  4. Raj Singh
    Replies:
    2
    Views:
    216
    Rick DeNatale
    May 29, 2008
  5. Gabriel Saravia

    Adding a =~ method to the Method class

    Gabriel Saravia, May 7, 2009, in forum: Ruby
    Replies:
    4
    Views:
    220
    Gabriel Saravia
    May 7, 2009
Loading...

Share This Page