Created
March 12, 2012 04:52
-
-
Save yesezra/2019882 to your computer and use it in GitHub Desktop.
It shouldn't be this hard
This file contains 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
module Hi | |
def self.included(base) | |
base.class_eval do #Enumerable | |
def hello | |
puts "hello" | |
end | |
end | |
end | |
end | |
module Enumerable | |
include Hi | |
end | |
[].hello # "hello" |
By using base.class_eval, the hello method is being defined in the context of the module or class that includes the Hi module. In this example, this means that the hello method is available on all instances of classes that include Enumerable – like Array, Hash, etc. This, in effect, is the same thing as adding the hello method definition directly to Enumerable.
thanks, got it.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
hi, quick random question. what does the def :hello accomplish when used this way inside a do-block code?