Redefine a Class?

Discussion in 'Ruby' started by Mark T, May 25, 2010.

  1. Mark T

    Mark T Guest

    Currently this raises: superclass mismatch for class Soda (TypeError)
    How can I redefine a class?
    As a class is an object, if I remove all of it's methods, attr & @'s
    could it be GC'd? (;)
    #-----------------------------------------
    class Soda
    def initialize
    @brand = "SweetenedSugar" end

    def retrieve_brand
    return @brand end end

    class Soda < String
    end
    #-----------------------------------------

    --
    MarkT
    Mark T, May 25, 2010
    #1
    1. Advertising

  2. Mark T wrote:
    > Currently this raises: superclass mismatch for class Soda (TypeError)
    > How can I redefine a class?
    > As a class is an object, if I remove all of it's methods, attr & @'s
    > could it be GC'd? (;)
    > #-----------------------------------------
    > class Soda
    > def initialize
    > @brand = "SweetenedSugar" end
    >
    > def retrieve_brand
    > return @brand end end
    >
    > class Soda < String
    > end
    > #-----------------------------------------


    What are your trying to do? You want to extend Stirng class? I ask,
    because at the moment I see that you are trying to define Soda class
    twice. First definition is a standalone class, and the second by
    extending String class.
    --
    Posted via http://www.ruby-forum.com/.
    Marcin Wolski, May 25, 2010
    #2
    1. Advertising

  3. Mark T

    Mark T Guest

    Hi Marcin,
    The String class is used as an example Class.
    Could be anything.
    Issue is in trying to (delete)/redefine a class.

    > What are your trying to do? You want to extend String class? I ask,


    MarkT
    Mark T, May 25, 2010
    #3
  4. Mark T wrote:
    > Hi Marcin,
    > The String class is used as an example Class.
    > Could be anything.
    > Issue is in trying to (delete)/redefine a class.
    >
    >> What are your trying to do? You want to extend String class? I ask,

    >
    > MarkT


    You can open existing class, and redefine methods. For example

    #defined Soda class
    class Soda
    def initialize
    @brand = "SweetenedSugar"

    end

    def retrieve_brand
    return @brand
    end
    end


    puts Soda.new.retrieve_brand
    #this will return "SweetenedSugar"


    #now, for example, you can open this class and redefine retrieve_brand
    method
    class Soda
    def retrieve_brand
    return @brand.upcase
    end
    end

    puts Soda.new.retrieve_brand
    #this will return "SWEETENEDSUGAR"


    Hope this is what you want?
    --
    Posted via http://www.ruby-forum.com/.
    Marcin Wolski, May 25, 2010
    #4
  5. Mark T

    Mark T Guest

    I'm looking for something... kinda 'destructive'....

    class Soda
    def initialize
    @brand = "SweetenedSugar" end

    def retrieve_brand
    return @brand end end

    Soda.removeClassAndMethods # yet to be (found) or
    implemented method.....

    irb(main):001:0> Soda.retrieve_brand
    NameError: uninitialized constant Soda
    from (irb):1
    #---------------------------- ^ yep, this error is what I'm looking for.....

    MarkT
    Mark T, May 25, 2010
    #5
  6. Mark T wrote:
    > I'm looking for something... kinda 'destructive'....
    >
    > class Soda
    > def initialize
    > @brand = "SweetenedSugar" end
    >
    > def retrieve_brand
    > return @brand end end
    >
    > Soda.removeClassAndMethods # yet to be (found) or
    > implemented method.....
    >
    > irb(main):001:0> Soda.retrieve_brand
    > NameError: uninitialized constant Soda
    > from (irb):1
    > #---------------------------- ^ yep, this error is what I'm looking
    > for.....
    >
    > MarkT


    You can remove a method as follows:

    class Soda
    remove_method :retrieve_brand
    end
    --
    Posted via http://www.ruby-forum.com/.
    Marcin Wolski, May 25, 2010
    #6
  7. Mark T

    Robert Dober Guest

    On Tue, May 25, 2010 at 9:18 AM, Mark T <> wrote:
    IIUC you want

    Soda = Class::new do
    def a; 42 end
    end

    Soda = Class::new( String ) do
    # Completely new class here, the old can indeed be GCed
    end

    You will get a const redefined warning, but if that hurts there are
    solutions, just probably out of topic here.

    HTH
    R.

    --
    The best way to predict the future is to invent it.
    -- Alan Kay
    Robert Dober, May 25, 2010
    #7
  8. On Tue, May 25, 2010 at 9:18 AM, Mark T <> wrote:
    > I'm looking for something... kinda 'destructive'....
    >
    > class Soda
    > def initialize
    > @brand =3D "SweetenedSugar" end
    >
    > def retrieve_brand
    > return @brand end end
    >
    > Soda.removeClassAndMethods =A0 =A0 =A0 =A0 =A0 # yet to be (found) or
    > implemented method.....
    >
    > irb(main):001:0> Soda.retrieve_brand
    > NameError: uninitialized constant Soda
    > =A0 =A0 =A0 =A0from (irb):1
    > #---------------------------- ^ yep, this error is what I'm looking for..=

    ...

    irb(main):001:0> class Soda
    irb(main):002:1> end
    =3D> nil
    irb(main):005:0> self.class.send:)remove_const,:Soda)
    =3D> Soda
    irb(main):006:0> Soda
    NameError: uninitialized constant Soda
    from (irb):6
    from :0
    irb(main):007:0> class Soda < String
    irb(main):008:1> end
    =3D> nil

    Jesus.
    Jesús Gabriel y Galán, May 25, 2010
    #8
  9. Mark T

    Mark T Guest

    Thanks all!!

    MarkT
    Mark T, May 25, 2010
    #9
  10. Mark T

    Robert Dober Guest

    2010/5/25 Jes=FAs Gabriel y Gal=E1n <>:

    > irb(main):005:0> self.class.send:)remove_const,:Soda)


    should that not rather be
    Object.send ....

    R.
    Robert Dober, May 25, 2010
    #10
  11. On Tue, May 25, 2010 at 4:56 PM, Robert Dober <> wrot=
    e:
    > 2010/5/25 Jes=FAs Gabriel y Gal=E1n <>:
    >
    >> irb(main):005:0> self.class.send:)remove_const,:Soda)

    >
    > should that not rather be
    > =A0 Object.send ....


    Could very well be :).
    I just saw that remove_const was a method in Module and I thought:
    self.class is a Module, so I'll use that.

    Jesus.
    Jesús Gabriel y Galán, May 25, 2010
    #11
  12. Mark T

    Robert Dober Guest

    2010/5/25 Jes=FAs Gabriel y Gal=E1n <>:

    > Could very well be :).
    > I just saw that remove_const was a method in Module and I thought:
    > self.class is a Module, so I'll use that.

    Where did I have my head? send remove_const to Object for top level
    constants and for all other to their defining module object, of
    course. Sorry got confused with irb output - that is waaay of a bad
    excuse I know ;)
    Cheers
    R.

    --=20
    The best way to predict the future is to invent it.
    -- Alan Kay
    Robert Dober, May 25, 2010
    #12
    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. Michael Bane
    Replies:
    0
    Views:
    577
    Michael Bane
    Sep 11, 2003
  2. Nafai
    Replies:
    4
    Views:
    333
    James Rafter
    Dec 8, 2004
  3. Replies:
    6
    Views:
    369
    Puppet_Sock
    Sep 9, 2005
  4. Stef Mientki
    Replies:
    1
    Views:
    231
    Arnaud Delobelle
    Nov 9, 2008
  5. Martin Boese

    Redefine a class with same name

    Martin Boese, Mar 9, 2008, in forum: Ruby
    Replies:
    2
    Views:
    92
    Martin Boese
    Mar 9, 2008
Loading...

Share This Page