-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmetaprogramming.rb
95 lines (75 loc) · 1.24 KB
/
metaprogramming.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
require 'pry'
require 'awesome_print'
AwesomePrint.pry!
# =========================
# Multiple classes
5.times do
class MYClass
puts 'Testing'
end
end
binding.pry
# obj = D.new
# obj.x
# obj.y
class D
def x
'x'
end
end
class D
def y
'y'
end
end
binding.pry
# =========================
# Instance_variables example
# obj = MyClass.new
# obj.instance_variables
# obj.my_method
# obj.methods.grep(/my/)
# var_name = 'v'
# obj.instance_variables.include?("@#{var_name}".to_sym)
# obj.instance_variable_set(:@topic, "Metaprogramming in Ruby")
# obj.instance_variable_get(:@topic)
class MyClass
def my_method
@v = 1
end
end
binding.pry
# =========================
# Constants
# MyModule::MyClass::MyConstant
# MyModule::MyConstant
# Module.nesting
# MyModule.constants
# Module.constants[0..1]
module MyModule
MyConstant = 'Outer constant'
class MyClass
MyConstant = 'Inner constant'
end
end
binding.pry
# =========================
#ancestors
class Abuelo
def metodo
end
end
class Padre < Abuelo
end
class Hijo < Padre
end
binding.pry
# =========================
#module inclusion
module Test
@coffee = true
end
class Dev
include Test
end
binding.pry