Feature #7019
closedallow `private` and `protected` keywords to take blocks
Added by alexeymuranov (Alexey Muranov) almost 12 years ago. Updated over 6 years ago.
Description
I like to indent my private methods one level deeper, but this indentation is inconsistent with the syntax:
class C
def f
#
end
private
def g
#
end
end
I think it would be nice if the private
keyword could take a block, then i would write:
class C
def f
#
end
private do
def g
#
end
end
end
Updated by trans (Thomas Sawyer) almost 12 years ago
I've often wondered about being able to use:
private def f
#
end
I think it's true that the public/private/protected "modifiers" have always felt a bit awkward --which accounts for the variety of indention and comment "highlights" that have been used with them.
Updated by shyouhei (Shyouhei Urabe) almost 12 years ago
I know you like blocks but this proposal doesn't make sense to me because public
/private
/protected
are declarations, not something done. So it's odd to say "private DO something
". I'd rather +1 to private def
, which is natural (to me at least).
Updated by alexeymuranov (Alexey Muranov) almost 12 years ago
What about
privately do
end
? :)
(I understand that a good reason is needed to add a new keyword).
Updated by alexeymuranov (Alexey Muranov) almost 12 years ago
Here is a usecase i have made up. The following does not work now:
class C
def f
def g
#
end
private
def h
#
end
end
end
c = C.new
c.f # => NameError: undefined local variable or method `private' for ...
So a block syntax could make sense here:
class C
def f
def g
#
end
privately do
def h
#
end
end
end
end
Updated by drbrain (Eric Hodel) almost 12 years ago
I don't know why you would expect this to work, private
, public
, etc. are methods on Module
and your context is not a subclass of Module
.
Why should we pollute every object with a new methods privately
, publicly
and protectedly
(which isn't a word)?
Updated by headius (Charles Nutter) almost 12 years ago
+1 for "private def foo
". I have never seen a good justification for why visibility is better as a value on the current frame, nor for why it's better to set a method's visibility after declaration rather than along with the declaration.
The fact that visibility lives on the frame also means implementations that attempt to optimize frames away (like JRuby does and eventually MRI will want to do) have a harder time of it.
Updated by luislavena (Luis Lavena) almost 12 years ago
- Category set to core
- Target version set to 2.0.0
headius (Charles Nutter) wrote:
+1 for "
private def foo
". I have never seen a good justification for why visibility is better as a value on the current frame, nor for why it's better to set a method's visibility after declaration rather than along with the declaration.
So two changes:
-
def
will require to return the method defined instead ofnil
-
private
will require to accept aMethod
and not just aSymbol
I believe these two changes where already requested (can't find the bug reports yet).
This approach helps also to describe methods beyond documentation markers, making it clear when a method is private inline with the method definition and not after the method has been defined.
I like this.
Updated by trans (Thomas Sawyer) almost 12 years ago
Would need to accept array of those too (whether its a method or symbol) for things like:
private attr_accessor :foo
Updated by alexeymuranov (Alexey Muranov) almost 12 years ago
Excuse me Thomas, what's the point in a "private attribute accessor
"?
Updated by trans (Thomas Sawyer) almost 12 years ago
Probably not much, but it is possible. Could be "protected
" instead as well. Also, other helpers can be created besides attr
. My point is only that it needs to accept an array should the helper return multiple methods/symbols.
Or are you thinking that private def
would be some kind of keyword thing?
Updated by ko1 (Koichi Sasada) almost 12 years ago
- Assignee set to mame (Yusuke Endoh)
mame-san, could you judge this ticket?
IMO, it is too slow to introduce such a big feature.
Updated by mame (Yusuke Endoh) almost 12 years ago
- Status changed from Open to Assigned
- Assignee changed from mame (Yusuke Endoh) to matz (Yukihiro Matsumoto)
- Target version changed from 2.0.0 to 3.0
IMO, it is too slow to introduce such a big feature.
Completely agreed.
--
Yusuke Endoh mame@tsg.ne.jp
Updated by alexeymuranov (Alexey Muranov) almost 12 years ago
Please do not forget also module_function
and such. (Or are they different in some sense?).
Alexey.
Updated by kou (Kouhei Sutou) over 6 years ago
- Status changed from Assigned to Rejected
#3753 adds private def ...
support. If you still want this feature, please reopen this issue.
Updated by shyouhei (Shyouhei Urabe) almost 5 years ago
- Has duplicate Feature #16276: For consideration: "private do...end" / "protected do...end" added