I’m fairly new to Ruby but I came across this weird gist where a function def code is written inside a do-block code. What is the purpose of this:
module Hi
def self.included(base)
base.class_eval do
def hello; puts 'Hello' end
end
end
end
I’m sure I should first get my Mixin concepts straight before moving ahead (and I’m in the process of diving deeper into mixin’s and whatnot) but the way def :hello is used inside the do-block is confusing me
What this particular bit of code does is a define a method (
hello) in any class thatincludes this module. Given this, the following two bits of code are (roughly) equivalent:You’ll find the documentation for
Module#class_evalandModule.includedinformative.