Forum: Ruby-core [CommonRuby - Feature #8635][Open] attr_accessor with default block

B397b498cc02503a2d86c86176f7fd3e?d=identicon&s=25 Magnus Holm (judofyr)
on 2013-07-14 17:00
(Received via mailing list)
Issue #8635 has been reported by judofyr (Magnus Holm).

----------------------------------------
Feature #8635: attr_accessor with default block
https://bugs.ruby-lang.org/issues/8635

Author: judofyr (Magnus Holm)
Status: Open
Priority: Normal
Assignee:
Category:
Target version:


=begin

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'
   end

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

   # (3) This proposal
   attr_accessor :name do
     'Hello'
   end
 end

 p = Person.new
 p.instance_variable_get(:@name) # => nil
 p.name # => '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

=end
Dddf1d0b08d492bbf18d7b865db098d8?d=identicon&s=25 rkh (Konstantin Haase) (Guest)
on 2013-07-15 18:08
(Received via mailing list)
Issue #8635 has been updated by rkh (Konstantin Haase).


If this should be added, could you consider adding it with a
read-write-lock?
----------------------------------------
Feature #8635: attr_accessor with default block
https://bugs.ruby-lang.org/issues/8635#change-40512

Author: judofyr (Magnus Holm)
Status: Open
Priority: Normal
Assignee:
Category:
Target version:


=begin

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'
   end

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

   # (3) This proposal
   attr_accessor :name do
     'Hello'
   end
 end

 p = Person.new
 p.instance_variable_get(:@name) # => nil
 p.name # => '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

=end
B397b498cc02503a2d86c86176f7fd3e?d=identicon&s=25 Magnus Holm (judofyr)
on 2013-07-16 12:50
(Received via mailing list)
On Mon, Jul 15, 2013 at 6:07 PM, rkh (Konstantin Haase) <me@rkh.im>
wrote:

>
> Issue #8635 has been updated by rkh (Konstantin Haase).
>
>
> If this should be added, could you consider adding it with a
> read-write-lock?


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
attr_accessor.

Or is it for detecting recursive calls in the same thread?
4dea430d31b993abaf41cd9b54f8128d?d=identicon&s=25 Avdi Grimm (avdi)
on 2013-07-16 17:35
(Received via mailing list)
Issue #8635 has been updated by avdi (Avdi Grimm).


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 https://github.com/ahoward/fattr, which is my
personal favorite and a gem I've used happily for many years. It might
provide some implementation inspiration.
----------------------------------------
Feature #8635: attr_accessor with default block
https://bugs.ruby-lang.org/issues/8635#change-40534

Author: judofyr (Magnus Holm)
Status: Open
Priority: Normal
Assignee:
Category:
Target version:


=begin

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'
   end

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

   # (3) This proposal
   attr_accessor :name do
     'Hello'
   end
 end

 p = Person.new
 p.instance_variable_get(:@name) # => nil
 p.name # => '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

=end
Fe1c0797f345249625590d25a51efd0a?d=identicon&s=25 prijutme4ty (Ilya Vorontsov) (Guest)
on 2013-07-22 23:01
(Received via mailing list)
Issue #8635 has been updated by prijutme4ty (Ilya Vorontsov).


May be thread-safety should be optional. But it definitely should be.
----------------------------------------
Feature #8635: attr_accessor with default block
https://bugs.ruby-lang.org/issues/8635#change-40609

Author: judofyr (Magnus Holm)
Status: Open
Priority: Normal
Assignee:
Category:
Target version:


=begin

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'
   end

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

   # (3) This proposal
   attr_accessor :name do
     'Hello'
   end
 end

 p = Person.new
 p.instance_variable_get(:@name) # => nil
 p.name # => '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

=end
This topic is locked and can not be replied to.