Guest User

Untitled

a guest
Apr 23rd, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.29 KB | None | 0 0
  1. # The meat of this code!
  2. module Goo
  3. module InstanceMethods
  4. def babble
  5. return "goozied my foozy!"
  6. end
  7. end
  8.  
  9. module ClassMethods
  10.  
  11. end
  12.  
  13. def self.included(base)
  14. if base.method_defined? :babble
  15. base.send(:remove_method, "babble")
  16. end
  17. base.extend ClassMethods
  18. base.send :include, InstanceMethods
  19. end
  20. end
  21.  
  22. class Foo
  23.  
  24. def babble
  25. return "foozy"
  26. end
  27.  
  28. end
  29.  
  30. # adding this method to Object so it'll run in textmate :)
  31. class << self
  32. # This is just a way to make this example print out nicer in a console!
  33. def should_be(control, test)
  34. if control == test
  35. puts "#{control}\t\t\t\t\t\t:-)"
  36. else
  37. puts "Should be '#{control}', not '#{test}'!\t:-("
  38. end
  39. end
  40. end
  41. # This is what I'm trying to do...
  42. a = Foo.new # Create an instance of Foo _before_ the behavior is augmented
  43. should_be 'foozy', a.babble # Demonstrate that the behavior is from the original class
  44. Foo.send(:include, Goo) # Augment the behavior of Foo with the Goo module
  45. b = Foo.new # Create a new instance of Foo with our augmented Goo behavior
  46. should_be 'goozied my foozy!', b.babble # Make sure our new instance of Foo has Goo behavior
Add Comment
Please, Sign In to add comment