Download - Removing Methods (MOTM 2010.01)
![Page 1: Removing Methods (MOTM 2010.01)](https://reader031.vdocument.in/reader031/viewer/2022022001/54bddd274a7959ad608b45a7/html5/thumbnails/1.jpg)
Removing MethodsRuby Method of the MonthKevin Munc => @muncman
![Page 2: Removing Methods (MOTM 2010.01)](https://reader031.vdocument.in/reader031/viewer/2022022001/54bddd274a7959ad608b45a7/html5/thumbnails/2.jpg)
Adding a Method
class String def ruby_rocks? true #dat endend
![Page 3: Removing Methods (MOTM 2010.01)](https://reader031.vdocument.in/reader031/viewer/2022022001/54bddd274a7959ad608b45a7/html5/thumbnails/3.jpg)
Fancy New Method
"rails".ruby_rocks?=> true
![Page 4: Removing Methods (MOTM 2010.01)](https://reader031.vdocument.in/reader031/viewer/2022022001/54bddd274a7959ad608b45a7/html5/thumbnails/4.jpg)
Remove It!class String remove_method :ruby_rocks?end
![Page 5: Removing Methods (MOTM 2010.01)](https://reader031.vdocument.in/reader031/viewer/2022022001/54bddd274a7959ad608b45a7/html5/thumbnails/5.jpg)
Remove It!class String remove_method :ruby_rocks?end"rails".ruby_rocks?NoMethodError: undefined method `ruby_rocks?' for "rails":String
![Page 6: Removing Methods (MOTM 2010.01)](https://reader031.vdocument.in/reader031/viewer/2022022001/54bddd274a7959ad608b45a7/html5/thumbnails/6.jpg)
A Redefined Method
class Crb def to_s "Thanks Randall!" endend
![Page 7: Removing Methods (MOTM 2010.01)](https://reader031.vdocument.in/reader031/viewer/2022022001/54bddd274a7959ad608b45a7/html5/thumbnails/7.jpg)
Redefinition in Action
columbusrb = Crb.new
![Page 8: Removing Methods (MOTM 2010.01)](https://reader031.vdocument.in/reader031/viewer/2022022001/54bddd274a7959ad608b45a7/html5/thumbnails/8.jpg)
Redefinition in Action
columbusrb = Crb.new
columbusrb.to_s
![Page 9: Removing Methods (MOTM 2010.01)](https://reader031.vdocument.in/reader031/viewer/2022022001/54bddd274a7959ad608b45a7/html5/thumbnails/9.jpg)
Redefinition in Action
columbusrb = Crb.new
columbusrb.to_s=> "Thanks Randall!"
![Page 10: Removing Methods (MOTM 2010.01)](https://reader031.vdocument.in/reader031/viewer/2022022001/54bddd274a7959ad608b45a7/html5/thumbnails/10.jpg)
Redefinition Removalclass Crb remove_method :to_send
Note that we didn’t have to get a new instance for the removal to take effect.
![Page 11: Removing Methods (MOTM 2010.01)](https://reader031.vdocument.in/reader031/viewer/2022022001/54bddd274a7959ad608b45a7/html5/thumbnails/11.jpg)
Redefinition Removalclass Crb remove_method :to_send
Note that we didn’t have to get a new instance for the removal to take effect.
columbusrb.to_s
![Page 12: Removing Methods (MOTM 2010.01)](https://reader031.vdocument.in/reader031/viewer/2022022001/54bddd274a7959ad608b45a7/html5/thumbnails/12.jpg)
Redefinition Removalclass Crb remove_method :to_send
Note that we didn’t have to get a new instance for the removal to take effect.
columbusrb.to_s=> "#<Crb:0x101651538>"
![Page 13: Removing Methods (MOTM 2010.01)](https://reader031.vdocument.in/reader031/viewer/2022022001/54bddd274a7959ad608b45a7/html5/thumbnails/13.jpg)
Inherited Methodror = “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.
![Page 14: Removing Methods (MOTM 2010.01)](https://reader031.vdocument.in/reader031/viewer/2022022001/54bddd274a7959ad608b45a7/html5/thumbnails/14.jpg)
Inherited Methodror = “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.
class String remove_method :to_send
![Page 15: Removing Methods (MOTM 2010.01)](https://reader031.vdocument.in/reader031/viewer/2022022001/54bddd274a7959ad608b45a7/html5/thumbnails/15.jpg)
Inherited Methodror = “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.
class String remove_method :to_sendror.to_s=> "#<String:0x101682b88>"
![Page 16: Removing Methods (MOTM 2010.01)](https://reader031.vdocument.in/reader031/viewer/2022022001/54bddd274a7959ad608b45a7/html5/thumbnails/16.jpg)
Removal Locationclass Object remove_method :to_send
Defined in Kernel.
Object mixes in Kernel.
![Page 17: Removing Methods (MOTM 2010.01)](https://reader031.vdocument.in/reader031/viewer/2022022001/54bddd274a7959ad608b45a7/html5/thumbnails/17.jpg)
Removal Locationclass Object remove_method :to_send
Defined in Kernel.
Object mixes in Kernel.
NameError: method `to_s' not defined in Object
![Page 18: Removing Methods (MOTM 2010.01)](https://reader031.vdocument.in/reader031/viewer/2022022001/54bddd274a7959ad608b45a7/html5/thumbnails/18.jpg)
Put It All Back
class Crb def to_s "Thanks Randall!" endend
Starting from a clean slate, so String still has to_s, etc.
![Page 19: Removing Methods (MOTM 2010.01)](https://reader031.vdocument.in/reader031/viewer/2022022001/54bddd274a7959ad608b45a7/html5/thumbnails/19.jpg)
Undefine the Redefine Different than removal!
class Crb undef_method :to_send
Interesting that the default output of to_s is present in the output.
![Page 20: Removing Methods (MOTM 2010.01)](https://reader031.vdocument.in/reader031/viewer/2022022001/54bddd274a7959ad608b45a7/html5/thumbnails/20.jpg)
Undefine the Redefine Different than removal!
class Crb undef_method :to_send
Interesting that the default output of to_s is present in the output.
columbusrb.to_s
![Page 21: Removing Methods (MOTM 2010.01)](https://reader031.vdocument.in/reader031/viewer/2022022001/54bddd274a7959ad608b45a7/html5/thumbnails/21.jpg)
Undefine the Redefine Different than removal!
class Crb undef_method :to_send
Interesting that the default output of to_s is present in the output.
columbusrb.to_sNoMethodError: undefined method `to_s' for #<Crb:0x101651538>
![Page 22: Removing Methods (MOTM 2010.01)](https://reader031.vdocument.in/reader031/viewer/2022022001/54bddd274a7959ad608b45a7/html5/thumbnails/22.jpg)
Definitely Undefinedclass String undef_method :to_send
Note that the undef also applied to subclasses of the type where the undef happened.
![Page 23: Removing Methods (MOTM 2010.01)](https://reader031.vdocument.in/reader031/viewer/2022022001/54bddd274a7959ad608b45a7/html5/thumbnails/23.jpg)
Definitely Undefinedclass String undef_method :to_send
"rails".to_sNoMethodError: undefined method `to_s' for "rails":String
Note that the undef also applied to subclasses of the type where the undef happened.
![Page 24: Removing Methods (MOTM 2010.01)](https://reader031.vdocument.in/reader031/viewer/2022022001/54bddd274a7959ad608b45a7/html5/thumbnails/24.jpg)
Defined Elsewhereclass Crb undef_method :object_idend
The undef succeeds because the method is available to class Crb.
![Page 25: Removing Methods (MOTM 2010.01)](https://reader031.vdocument.in/reader031/viewer/2022022001/54bddd274a7959ad608b45a7/html5/thumbnails/25.jpg)
Defined Elsewhereclass Crb undef_method :object_idend
columbusrb = Crb.newcolumbusrb.object_idNoMethodError: undefined method `object_id' for #<Crb:0x10166d7d8>
The undef succeeds because the method is available to class Crb.
![Page 26: Removing Methods (MOTM 2010.01)](https://reader031.vdocument.in/reader031/viewer/2022022001/54bddd274a7959ad608b45a7/html5/thumbnails/26.jpg)
Method Removalremove_method :symbol
Removed from the specific type only.
Ruby still searches up the hierarchy.
undef_method :symbolPrevents calls to the method regardless of the type’s hierarchy.
Both defined in the Module class.
![Page 27: Removing Methods (MOTM 2010.01)](https://reader031.vdocument.in/reader031/viewer/2022022001/54bddd274a7959ad608b45a7/html5/thumbnails/27.jpg)
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).
![Page 28: Removing Methods (MOTM 2010.01)](https://reader031.vdocument.in/reader031/viewer/2022022001/54bddd274a7959ad608b45a7/html5/thumbnails/28.jpg)
Questions?Photo Credits:
http://www.flickr.com/photos/tomscott/4225623/http://www.flickr.com/photos/slayer23/2215384832/
http://www.flickr.com/photos/andreweason/268627274/http://www.flickr.com/photos/tomscott/28188883/http://www.flickr.com/photos/jonmikel/259535350/