Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Removing Methods (MOTM 2010.01)

894 views

Published on

Ruby Method of the Month 2010.01. Lightning talk for the Columbus Ruby Brigade.

Published in: Technology
  • Be the first to like this

Removing Methods (MOTM 2010.01)

  1. 1. Removing Methods Ruby Method of the Month Kevin Munc => @muncman
  2. 2. Adding a Method class String def ruby_rocks? true #dat end end
  3. 3. Fancy New Method "rails".ruby_rocks? => true
  4. 4. Remove It! class String remove_method :ruby_rocks? end
  5. 5. Remove It! class String remove_method :ruby_rocks? end "rails".ruby_rocks? NoMethodError: undefined method `ruby_rocks?' for "rails":String
  6. 6. A Redefined Method class Crb def to_s "Thanks Randall!" end end
  7. 7. Redefinition in Action columbusrb = Crb.new
  8. 8. Redefinition in Action columbusrb = Crb.new columbusrb.to_s
  9. 9. Redefinition in Action columbusrb = Crb.new columbusrb.to_s => "Thanks Randall!"
  10. 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. 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. 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. 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. 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. 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>"
  16. 16. Removal Location class Object remove_method :to_s end Defined in Kernel. Object mixes in Kernel.
  17. 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. 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. 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. 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. 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. 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. 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. 24. Defined Elsewhere class Crb undef_method :object_id end The undef succeeds because the method is available to class Crb.
  25. 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. 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. 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).
  28. 28. 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/

×