-
Notifications
You must be signed in to change notification settings - Fork 0
/
dynamic_methods.rb
82 lines (70 loc) · 1.18 KB
/
dynamic_methods.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
# Defines the methods normally
module Foo
def my_method
'text'
end
end
# Defines the methods using class_eval
module ClassFoo
class_eval <<-RUBY
def my_method
'text'
end
RUBY
end
# Defines the methods using define_method
module DefineFoo
define_method 'my_method' do
'text'
end
end
# Define method with a Proc
module ProcFoo
P = proc { 'proc method' }
define_method :my_method, P
end
# Defines a method that uses a block to get its job done
module BlockFoo
class_eval <<-RUBY
def my_method
yield
end
RUBY
end
# Base Class
class MyClass
def my_method
'text'
end
end
# Subclass normal methods
class MySubClass < MyClass
include Foo
end
# class using class eval methods
class CEClass < MyClass
include ClassFoo
end
# Class using define method methods
class DMClass < MyClass
include DefineFoo
end
# Class using defined method methods but prepended
class PreDMClass < MyClass
prepend DefineFoo
end
class ProcClass < MyClass
include ProcFoo
end
class BlockClass < MyClass
include BlockFoo
end
class InstanceEvalClass < MyClass
def initialize
self.instance_eval {
def my_method
'text'
end
}
end
end