Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # The meat of this code!
- module Goo
- module InstanceMethods
- def babble
- return "goozied my foozy!"
- end
- end
- module ClassMethods
- end
- def self.included(base)
- if base.method_defined? :babble
- base.send(:remove_method, "babble")
- end
- base.extend ClassMethods
- base.send :include, InstanceMethods
- end
- end
- class Foo
- def babble
- return "foozy"
- end
- end
- # adding this method to Object so it'll run in textmate :)
- class << self
- # This is just a way to make this example print out nicer in a console!
- def should_be(control, test)
- if control == test
- puts "#{control}\t\t\t\t\t\t:-)"
- else
- puts "Should be '#{control}', not '#{test}'!\t:-("
- end
- end
- end
- # This is what I'm trying to do...
- a = Foo.new # Create an instance of Foo _before_ the behavior is augmented
- should_be 'foozy', a.babble # Demonstrate that the behavior is from the original class
- Foo.send(:include, Goo) # Augment the behavior of Foo with the Goo module
- b = Foo.new # Create a new instance of Foo with our augmented Goo behavior
- 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