Feature #8635

attr_accessor with default block

Added by judofyr (Magnus Holm) over 7 years ago. Updated over 7 years ago.

Target version:



It's quite common to define attributes (like attr_reader, attr_accessor) with default values. It would be useful if Ruby provided a helper method for this case. attr_accessor and attr_reader can support this nicely using a default block:

class Person
# (1) Simple approach
attr_writer :name
def name
@name ||= 'Hello'

# (2) nil-safe approach
attr_writer :name
def name
return @name if defined? @name
@name = 'Hello'

# (3) This proposal
attr_accessor :name do

p =
p.instance_variable_get(:@name) # => nil # => 'Hello'
p.instance_variable_get(:@name) # => 'Hello'

Problems with current approaches:

  • The reader and the writer looks widely different
  • Solution 1 doesn't work as intended when the default value may evaulate to nil/false
  • Solution 2 requires you to write the attribute name five times


Updated by rkh (Konstantin Haase) over 7 years ago

If this should be added, could you consider adding it with a read-write-lock?

Updated by judofyr (Magnus Holm) over 7 years ago

On Mon, Jul 15, 2013 at 6:07 PM, rkh (Konstantin Haase) wrote:

Issue #8635 has been updated by rkh (Konstantin Haase).

If this should be added, could you consider adding it with a

What do you mean?

Is it for thread-safety? In that case: I disagree. Concurrent classes needs
to be specifically designed, and I don't want to add any overhead to

Or is it for detecting recursive calls in the same thread?

Updated by avdi (Avdi Grimm) over 7 years ago

Just adding some prior art...

There have been many, many takes on this in various gems, but I thought I'd drop in a note about, which is my personal favorite and a gem I've used happily for many years. It might provide some implementation inspiration.

Updated by prijutme4ty (Ilya Vorontsov) over 7 years ago

May be thread-safety should be optional. But it definitely should be.

Also available in: Atom PDF