Feature #6470

Make attr_accessor return the list of generated method

Added by Robert Pankowecki almost 2 years ago. Updated over 1 year ago.

[ruby-core:45144]
Status:Assigned
Priority:Normal
Assignee:Yukihiro Matsumoto
Category:-
Target version:next minor

Description

attr_accesor currently returns nil. It would be more helpful if it return list of generated methods so that it can become an argument to other methods like :private or :protected. That way private accessors can still be defined at top of the class and be private without changing the visibility of next methods.

class Something
private *attraccessor :user, :action # IMHO This is nice
# private attr
accessor :user, :action # <-- would be even better if :private method accepted arrays

def initialize(user, action)
self.user = user
self.action = action
end

def publicmethod
user.do
something(action)
end
end

VS

class Something
private; attr_accessor :user, :action; public # IMHO Hack!!

def initialize(user, action)
self.user = user
self.action = action
end

def publicmethod
user.do
something(action)
end
end

VS

class Something
def initialize(user, action)
self.user = user
self.action = action
end

def publicmethod
user.do
something(action)
end

private
attr_accessor :user, :action # IMHO Does not look nice at bottom of the class definition
end


Related issues

Duplicates ruby-trunk - Feature #6198: public/protected/private with attr_* Assigned 03/25/2012

History

#1 Updated by Henry Maddocks almost 2 years ago

Aren't accessors public by definition? If you want them to be private use attr.

#2 Updated by Robert A. Heiler almost 2 years ago

Yes, they are public.

If I understood them correctly, they are the same as this in pure ruby code:

attr_accessor :foo

def foo
@foo
end

def foo=(i)
@foo = i
end

I found that I personally only need attrreader, attrwriter and attr_accessor.
I have not found a use case for attr alone yet.

#3 Updated by Yusuke Endoh almost 2 years ago

  • Status changed from Open to Assigned
  • Assignee set to Yukihiro Matsumoto

This duplicates #6198.

BTW, why don't you use instance variables directly? That is:

class Something
def initialize(user, action)
@user = user
@action = action
end

def publicmethod
@user.do
something(@action)
end
end

Yusuke Endoh mame@tsg.ne.jp

#4 Updated by Robert Pankowecki almost 2 years ago

I want to access my private fields also via methods instead of directly via instance variables so refactoring in future is easier. For example instead of finding in class all occurrences of @var = something and changing them into either "@var = something.strip" or extracting it into setter, I already use the private setter defined with attr_accessor everywhere. That way I only need to change the setter implementation in one place and don't need to look for code setting instance variable because there is non such, only calls for the accessor.

#5 Updated by Yusuke Endoh over 1 year ago

  • Target version set to next minor

Also available in: Atom PDF