Your SlideShare is downloading. ×
0
Removing Methods
 Ruby Method of the Month
  Kevin Munc => @muncman
Adding a Method

class String
  def ruby_rocks?
    true #dat
  end
end
Fancy New Method


"rails".ruby_rocks?
      => true
Remove It!
class String
  remove_method :ruby_rocks?
end
Remove It!
class String
  remove_method :ruby_rocks?
end
"rails".ruby_rocks?
NoMethodError: undefined method
`ruby_rocks?'...
A Redefined Method

class Crb
  def to_s
    "Thanks Randall!"
  end
end
Redefinition in
        Action
columbusrb = Crb.new
Redefinition in
        Action
columbusrb = Crb.new

columbusrb.to_s
Redefinition in
        Action
columbusrb = Crb.new

columbusrb.to_s
=> "Thanks Randall!"
Redefinition Removal
class Crb
  remove_method :to_s
end
                        Note that we didn’t have
                ...
Redefinition Removal
class Crb
  remove_method :to_s
end

columbusrb.to_s
                        Note that we didn’t have...
Redefinition Removal
class Crb
  remove_method :to_s
end

columbusrb.to_s
                          Note that we didn’t ha...
Inherited Method
ror = “Ruby on Rails”
ror.to_s
=> "Ruby on Rails"


                        We get the to_s from Object.
...
Inherited Method
ror = “Ruby on Rails”
ror.to_s
=> "Ruby on Rails"
class String
  remove_method :to_s   We get the to_s fr...
Inherited Method
ror = “Ruby on Rails”
ror.to_s
=> "Ruby on Rails"
class String
  remove_method :to_s    We get the to_s f...
Removal Location
class Object
  remove_method :to_s
end


                        Defined in Kernel.

                     ...
Removal Location
class Object
  remove_method :to_s
end

NameError: method `to_s' not
defined in Object       Defined in Ke...
Put It All Back
           Starting from a clean slate, so String still has to_s, etc.




class Crb
  def to_s
    "Thank...
Undefine the
        Redefine       Different than removal!




class Crb
  undef_method :to_s
end
                       ...
Undefine the
        Redefine       Different than removal!




class Crb
  undef_method :to_s
end
                       ...
Undefine the
         Redefine          Different than removal!




class Crb
  undef_method :to_s
end
                   ...
Definitely Undefined
class String
  undef_method :to_s
end
                       Note that the undef also applied
       ...
Definitely Undefined
class String
  undef_method :to_s
end
                         Note that the undef also applied
     ...
Defined Elsewhere
class Crb
  undef_method :object_id
end


                            The undef succeeds
               ...
Defined Elsewhere
class Crb
  undef_method :object_id
end

columbusrb = Crb.new        The undef succeeds
                ...
Method Removal
remove_method :symbol
 Removed from the specific type only.

 Ruby still searches up the hierarchy.

undef_...
Finer Points
  (pun intended)
You can call remove_method only
on a method actually defined
within the class itself.

You c...
Questions?
                Photo Credits:
    http://www.flickr.com/photos/tomscott/4225623/
 http://www.flickr.com/photos...
Upcoming SlideShare
Loading in...5
×

Removing Methods (MOTM 2010.01)

641

Published on

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

Published in: Technology
2 Comments
0 Likes
Statistics
Notes
  • Be the first to like this

No Downloads
Views
Total Views
641
On Slideshare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
3
Comments
2
Likes
0
Embeds 0
No embeds

No notes for slide




















  • Transcript of "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/
    1. A particular slide catching your eye?

      Clipping is a handy way to collect important slides you want to go back to later.

    ×