Redefine a Class?

M

Mark T

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
#-----------------------------------------
 
M

Marcin Wolski

Mark said:
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.
 
M

Mark T

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
 
M

Marcin Wolski

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


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?
 
M

Mark T

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
 
M

Marcin Wolski

Mark said:
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
 
R

Robert Dober

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.
 
J

Jesús Gabriel y Galán

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.
 
J

Jesús Gabriel y Galán

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.
 
R

Robert Dober

2010/5/25 Jes=FAs Gabriel y Gal=E1n said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,141
Latest member
BlissKeto
Top