Advertisement
Guest User

Untitled

a guest
May 30th, 2015
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.90 KB | None | 0 0
  1. # Tuesday: Methods
  2.  
  3. # Messages
  4. # Methods aren't actually called directly on an object.
  5. # A message is sent to the *receiver*, which then looks for a method with that message name.
  6.  
  7. # When you call a method, it looks a little more like this on the back end:
  8.  
  9. # call
  10. receiver.my_method
  11.  
  12. # explicit
  13. receiver.send(:my_method) #send message
  14. receiver.method(:my_method).call #find method on receiver and call it
  15.  
  16. # Method Clashing
  17.  
  18. # If a method is defined anywhere in the object chain, method_missing won't catch it.
  19. # There are two ways to undefine a method
  20.  
  21. class K
  22. end
  23.  
  24. k = K.new
  25. k.class # => K
  26.  
  27. class K
  28. instance_methods.each do |m|
  29. undef_method(m) if m == :class
  30. end
  31. end
  32.  
  33. k = K.new
  34. k.class
  35.  
  36. k.class # => undefined method 'class'
  37.  
  38. class J
  39. def class
  40. puts 'JJ'
  41. end
  42. end
  43.  
  44. j = J.new
  45. j.class # => JJ
  46.  
  47. class J
  48. instance_methods.each do |m|
  49. remove_method(m) if m == :class
  50. end
  51. end
  52.  
  53. j = J.new
  54. j.class # => J
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement