-
Notifications
You must be signed in to change notification settings - Fork 0
/
attr_accessor.rb
88 lines (70 loc) · 2.01 KB
/
attr_accessor.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
# Source: https://dmitrytsepelev.dev/attr-accessor-in-ruby
# Takeaways
# 1. attr_accessor is a method that sits in the singleton class and,
# when called, defines methods dynamically
# 2. Using a benchmark to compare attr_accessor with methods written by hand, we see that
# writer is 34% faster, while reader shows 18% better performance
# 1. attr_accessor is a method that sits in the singleton class and,
# when called, defines methods dynamically
puts 'attr_accessor is a method that sits in the singleton class and,
# when called, defines methods dynamically'
# Custom Attribute Accessor
module MyAttrAccessor
def my_attr_accessor(attr_name)
my_attr_reader(attr_name)
my_attr_writer(attr_name)
end
def my_attr_reader(attr_name)
instance_variable_name = attr_to_variable_name(attr_name)
define_method(attr_name) do
instance_variable_get(instance_variable_name)
end
end
def my_attr_writer(attr_name)
instance_variable_name = attr_to_variable_name(attr_name)
define_method("#{attr_name}=") do |value|
instance_variable_set(instance_variable_name, value)
end
end
private
def attr_to_variable_name(attr_name)
"@#{attr_name}"
end
end
Object.extend(MyAttrAccessor)
# 2. Using a benchmark to compare attr_accessor with methods written by hand, we see that
# writer is 34% faster, while reader shows 18% better performance
puts "\n Benchmark to compare attr_accessor with methods written by hand"
require "benchmark"
class NativeAccessor
attr_accessor :value
end
class CustomAccessor
def value
@value
end
def value=(v)
@value = v
end
end
n = 10_000_000
Benchmark.bm do |x|
na = NativeAccessor.new
x.report("attr_writer") do
n.times { |i| na.value = i }
end
ca = CustomAccessor.new
x.report("value=") do
n.times { |i| ca.value = i }
end
end
Benchmark.bm do |x|
na = NativeAccessor.new
x.report("attr_reader") do
n.times { |i| na.value }
end
ca = CustomAccessor.new
x.report("value") do
n.times { |i| ca.value }
end
end