Silly question

Is there any way of completely remove a class object’s methods instead of
extending the class?

I’d like to be able to do:

class A
attr :t
def initialize
@t = 1
end
end

a = A.new
puts a.t # => 1

Not extending… redefining here…

class A
attr :x
def initialize
@x = 3
end
end

a = A.new
puts a.x # => 3
puts a.t # => error, instead of outputing nil

I’m finding the above to be a major pain when debugging and prototyping new
code in either irb or an embedded ruby, every time I reload a library.

Is there any way of completely remove a class object’s methods instead of
extending the class?
[snip]
I’m finding the above to be a major pain when debugging and prototyping new
code in either irb or an embedded ruby, every time I reload a library.

Will an ‘alias’ help you? like this

ruby a.rb
1
3
1
cat a.rb
class A
attr :t
def initialize
@t = 1
end
end
a = A.new
puts a.t # => 1

Not extending… redefining here…

class A
attr :x
alias old_initialize initialize
def initialize
old_initialize
@x = 3
end
end
a = A.new
puts a.x # => 3
puts a.t # => 1

···

On Wed, 31 Mar 2004 07:20:18 +0000, GGarramuno wrote:


Simon Strandgaard

Is there any way of completely remove a class object’s methods instead of
extending the class?

I’d like to be able to do:

class A
attr :t
def initialize
@t = 1
end
end

a = A.new
puts a.t # => 1

Not extending… redefining here…

class A
attr :x
def initialize
@x = 3
end
end

a = A.new
puts a.x # => 3
puts a.t # => error, instead of outputing nil

I’m finding the above to be a major pain when debugging and prototyping new
code in either irb or an embedded ruby, every time I reload a library.

What version are you using I have 1.8.1 and for me that code outputs:
1
./first.rb:20: warning: method redefined; discarding old initialize
3
nil

line 20 is the second “def initialise” line and the warning goes away
when I dont run it with “-w”.

/Stefan

Is there any way of completely remove a class object’s methods instead of
extending the class?

I’d like to be able to do:

class A
attr :t
def initialize
@t = 1
end
end

a = A.new
puts a.t # => 1

Not extending… redefining here…

this works (with warnings)

class A
attr :t
def initialize
@t=1
end
end
=> nil
class B
attr :x
def initialize
@x=3
end
end
=> nil
Object::const_set(‘A’,B)
(irb):13: warning: already initialized constant A
=> B
A.new.t
NoMethodError: undefined method `t’ for #<B:0x27c8520 @x=3>
from (irb):14

···

il 31 Mar 2004 06:20:18 GMT, ggarramuno@aol.com (GGarramuno) ha scritto::

“Simon Strandgaard” neoneye@adslhome.dk schrieb im Newsbeitrag
news:pan.2004.03.31.06.29.43.124211@adslhome.dk…

Is there any way of completely remove a class object’s methods instead
of
extending the class?
[snip]
I’m finding the above to be a major pain when debugging and
prototyping new
code in either irb or an embedded ruby, every time I reload a library.

Will an ‘alias’ help you? like this

ruby a.rb
1
3
1
cat a.rb
class A
attr :t
def initialize
@t = 1
end
end
a = A.new
puts a.t # => 1

Not extending… redefining here…

class A
attr :x
alias old_initialize initialize
def initialize
old_initialize
@x = 3
end
end
a = A.new
puts a.x # => 3
puts a.t # => 1

I don’t think that’s what the OP wanted. This seems to be more
appropriate

class A
attr :t
def initialize
@t = 1
end
end

a = A.new
puts a.t # => 1

Not extending… redefining here…

class A
remove_method :t
attr :x
def initialize
@x = 3
end
end

a = A.new
puts a.x # => 3
puts a.t # => error, instead of outputing nil

yields

irb(main):022:0> puts a.t # => error, instead of outputing nil
NoMethodError: undefined method `t’ for #<A:0x101a6448 @x=3>
from (irb):22

Regards

robert
···

On Wed, 31 Mar 2004 07:20:18 +0000, GGarramuno wrote:
from :0

I don’t think that’s what the OP wanted. This seems to be more
appropriate

Indeed. remove_method and undef_method is what I want.

Is there a particular reason there’s no remove_instance_variable to match all
the other remove_ methods of Module?

Hi --

···

On Thu, 1 Apr 2004, GGarramuno wrote:

>
>I don't think that's what the OP wanted. This seems to be more
>appropriate

Indeed. remove_method and undef_method is what I want.

Is there a particular reason there's no remove_instance_variable to match all
the other remove_ methods of Module?

I don't think it would match them, in that instance variables are not
governed by or stored in Module objects. So it would be a different
thing -- undefining a variable.

David

--
David A. Black
dblack@wobblini.net

“GGarramuno” ggarramuno@aol.com schrieb im Newsbeitrag
news:20040331173236.20738.00000341@mb-m19.aol.com

I don’t think that’s what the OP wanted. This seems to be more
appropriate

Indeed. remove_method and undef_method is what I want.

Is there a particular reason there’s no remove_instance_variable to
match all
the other remove_ methods of Module?

There is, but it’s private:

class Foo; attr_accessor :bar; end
=> nil
f = Foo.new
=> #Foo:0x10186cb8
f.bar = “foo”
=> “foo”
f
=> #<Foo:0x10186cb8 @bar=“foo”>
f.remove_instance_variable :@bar
NoMethodError: private method `remove_instance_variable’ called for
#<Foo:0x10186cb8 @bar=“foo”>
from (irb):5
class Foo
def rem(sym)
remove_instance_variable sym
end
end
=> nil
f.rem :@bar
=> “foo”
f
=> #Foo:0x10186cb8

:slight_smile:

robert