-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmethod_missing.rb
More file actions
29 lines (27 loc) · 820 Bytes
/
method_missing.rb
File metadata and controls
29 lines (27 loc) · 820 Bytes
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
#class that dynamically defines properties and defaults them to 0.0 on the fly
class DynAttrClass
def method_missing(sym, *args, &block)
name_of_method = sym.to_s
attr_name = ""
(class << self; self; end).class_eval do
if (name_of_method[-1] == "=") then
#define setter and instance variable
attr_name = "@#{name_of_method[0..-2]}"
define_method sym do |*args|
instance_variable_set(attr_name, args[0])
end
else
#define getter
attr_name = "@" + name_of_method
define_method sym do
instance_variable_get(attr_name)
end
end
end
if instance_variable_get(attr_name).nil?
instance_variable_set(attr_name, 0.0)
end
#after we define send the message
send sym, *args
end
end