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
#-----------------------------------------
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 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.
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?
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 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
On Tue, May 25, 2010 at 9:18 AM, Mark T [email protected] 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.
On Tue, May 25, 2010 at 9:18 AM, Mark T [email protected] wrote:
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…
irb(main):001:0> class Soda
irb(main):002:1> end
=> nil
irb(main):005:0> self.class.send(:remove_const,:Soda)
=> 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
=> nil
Jesus.
2010/5/25 Jesús Gabriel y Galán [email protected]:
irb(main):005:0> self.class.send(:remove_const,:Soda)
should that not rather be
Object.send …
R.
On Tue, May 25, 2010 at 4:56 PM, Robert D. [email protected]
wrote:
2010/5/25 Jesús Gabriel y Galán [email protected]:
irb(main):005:0> self.class.send(:remove_const,:Soda)
should that not rather be
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.
2010/5/25 Jesús Gabriel y Galán [email protected]:
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.