-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmetaprogramming2.rb
78 lines (41 loc) · 1004 Bytes
/
metaprogramming2.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# using modules in metaprogramming
# when a module is included, all instance methods are now available in the included class
module SuperPowers
def self.included(klass)
puts "Hey, I've been included in the #{klass}."
end
def jump
puts "you are jumping"
end
end
# ====================================================================================
# include for instance methods, extend for class methods
module UsefulInstanceMethods
def an_instance_method
puts "an instance method module"
end
end
module UsefulClassMethods
def a_class_method
puts "a class method module"
end
end
class Host
include UsefulInstanceMethods
extend UsefulClassMethods
end
# now, mix these both together for more elegant code, knocking off the extra step:
module UsefulMethods
module ClassMethods
def a_class_method
end
end
def self.included(host_class)
host_class.extend(ClassMethods)
end
def an_instance_method
end
end
class Host
include UsefulMethods
end