Archive for February 19, 2018
-
Ruby performance: #attr_accessor vs. method definition
February 19, 2018
I’ve been digging a lot into Ruby performance lately and this much digging has taken me into some very interesting corners of Rubyland. My latest surprise has been the difference between defining attribute methods via the
attr_accessor
provided language construct (as well asattr_reader
andattr_writer
) vs. defining them yourself (as indef attribute
anddef attribute=
). Here’s what I ran into…I created two simple classes with identical interfaces. The only difference between the two is how we are defining access to set and get the
@value
attribute in each:class TestClassAttrAccessor attr_accessor :value def initialize value @value = value end end class TestClassDefMethod def initialize value @value = value end def