Created
May 11, 2017 01:03
-
-
Save metaskills/5e062c74b87f176246e4b7ac5f10d541 to your computer and use it in GitHub Desktop.
Using Module Prepend To Extend Ruby Classes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Foo | |
attr_reader :bar | |
def initialize | |
@bar = 'bar' | |
end | |
end | |
module Bar | |
def bar | |
super.upcase | |
end | |
end | |
Foo.prepend Bar | |
Foo.new.bar # => 'BAR' | |
# If include was used: | |
Foo.include Bar | |
Foo.new.bar # => 'bar' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment