Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Tuesday: Methods
- # Messages
- # Methods aren't actually called directly on an object.
- # A message is sent to the *receiver*, which then looks for a method with that message name.
- # When you call a method, it looks a little more like this on the back end:
- # call
- receiver.my_method
- # explicit
- receiver.send(:my_method) #send message
- receiver.method(:my_method).call #find method on receiver and call it
- # Method Clashing
- # If a method is defined anywhere in the object chain, method_missing won't catch it.
- # There are two ways to undefine a method
- class K
- end
- k = K.new
- k.class # => K
- class K
- instance_methods.each do |m|
- undef_method(m) if m == :class
- end
- end
- k = K.new
- k.class
- k.class # => undefined method 'class'
- class J
- def class
- puts 'JJ'
- end
- end
- j = J.new
- j.class # => JJ
- class J
- instance_methods.each do |m|
- remove_method(m) if m == :class
- end
- end
- j = J.new
- j.class # => J
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement