10. Redefinition Removal
class Crb
remove_method :to_s
end
Note that we didn’t have
to get a new instance for
the removal to take
effect.
11. Redefinition Removal
class Crb
remove_method :to_s
end
columbusrb.to_s
Note that we didn’t have
to get a new instance for
the removal to take
effect.
12. Redefinition Removal
class Crb
remove_method :to_s
end
columbusrb.to_s
Note that we didn’t have
to get a new instance for
the removal to take
effect.
=> "#<Crb:0x101651538>"
13. Inherited Method
ror = “Ruby on Rails”
ror.to_s
=> "Ruby on Rails"
We get the to_s from Object.
Note that the removal takes
effect for instances already
declared.
14. Inherited Method
ror = “Ruby on Rails”
ror.to_s
=> "Ruby on Rails"
class String
remove_method :to_s We get the to_s from Object.
end Note that the removal takes
effect for instances already
declared.
15. Inherited Method
ror = “Ruby on Rails”
ror.to_s
=> "Ruby on Rails"
class String
remove_method :to_s We get the to_s from Object.
end Note that the removal takes
effect for instances already
declared.
ror.to_s
=> "#<String:0x101682b88>"
17. Removal Location
class Object
remove_method :to_s
end
NameError: method `to_s' not
defined in Object Defined in Kernel.
Object mixes in Kernel.
18. Put It All Back
Starting from a clean slate, so String still has to_s, etc.
class Crb
def to_s
"Thanks Randall!"
end
end
19. Undefine the
Redefine Different than removal!
class Crb
undef_method :to_s
end
Interesting that the
default output of to_s
is present in the output.
20. Undefine the
Redefine Different than removal!
class Crb
undef_method :to_s
end
Interesting that the
default output of to_s
is present in the output.
columbusrb.to_s
21. Undefine the
Redefine Different than removal!
class Crb
undef_method :to_s
end
Interesting that the
default output of to_s
is present in the output.
columbusrb.to_s
NoMethodError: undefined method
`to_s' for #<Crb:0x101651538>
22. Definitely Undefined
class String
undef_method :to_s
end
Note that the undef also applied
to subclasses of the type where
the undef happened.
23. Definitely Undefined
class String
undef_method :to_s
end
Note that the undef also applied
to subclasses of the type where
the undef happened.
"rails".to_s
NoMethodError: undefined method
`to_s' for "rails":String
24. Defined Elsewhere
class Crb
undef_method :object_id
end
The undef succeeds
because the method is
available to class Crb.
25. Defined Elsewhere
class Crb
undef_method :object_id
end
columbusrb = Crb.new The undef succeeds
because the method is
columbusrb.object_id available to class Crb.
NoMethodError: undefined method
`object_id' for #<Crb:0x10166d7d8>
26. Method Removal
remove_method :symbol
Removed from the specific type only.
Ruby still searches up the hierarchy.
undef_method :symbol
Prevents calls to the method regardless
of the type’s hierarchy.
Both defined in the Module class.
27. Finer Points
(pun intended)
You can call remove_method only
on a method actually defined
within the class itself.
You can call undef_method on a
method accessible from the
class (inherited or mixed in).