-
Notifications
You must be signed in to change notification settings - Fork 0
/
module.rb
72 lines (59 loc) · 1.07 KB
/
module.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# module M
# def method_a
# __method__
# end
# end
# class C
# include M
# end
# class C
# def method_b
# __method__
# end
# end
# module M
# def method_b
# __method__
# end
# end
# p C.new.method_a
# p C.new.method_b
# p C.new.public_methods.include? :method_a #=> true
# p C.new.public_methods.include? :method_b #=> true
# =====NEXT QUESTION=====
# module M
# def self.foo
# [1,2,3].each do |i|
# bar
# end
# end
# def self.bar
# p "bar invoked!!"
# end
# end
# M.foo
# "bar invoked!!"
# "bar invoked!!"
# "bar invoked!!"
# =====NEXT QUESTION=====
class C
end
module M
def self.foo
puts "self in module M is: #{self}"
wrapper_module = self
C.send(:define_method, :bar) do |*args, **kwargs|
puts "args is: #{args}"
puts "kwargs is: #{kwargs}"
puts "self is: #{self}"
puts "wrapper_module is: #{wrapper_module}"
end
end
end
M.foo
#=> self in module M is: M
C.new.bar(1,2,3, a: 1)
# args is: [1, 2, 3]
# kwargs is: {:a=>1}
# self is: #<C:0x00007fae779cb990>
# wrapper_module is: M