Feature #7019

allow `private` and `protected` keywords to take blocks

Added by Alexey Muranov over 2 years ago. Updated 3 months ago.

[ruby-core:47505]
Status:Assigned
Priority:Normal
Assignee:Yukihiro Matsumoto

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

Related issues

Duplicated by Ruby trunk - Feature #8497: private, protected, private_constなどがブロックを受け取るようにする Open 06/05/2013

History

#1 Updated by Thomas Sawyer over 2 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.

#2 Updated by Shyouhei Urabe over 2 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).

#3 Updated by Alexey Muranov over 2 years ago

What about

privately do
end

? :)
(I understand that a good reason is needed to add a new keyword).

#4 Updated by Alexey Muranov over 2 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

#5 Updated by Eric Hodel over 2 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)?

#6 Updated by Charles Nutter over 2 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.

#7 Updated by Luis Lavena over 2 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 of nil
  • private will require to accept a Method and not just a Symbol

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.

#8 Updated by Thomas Sawyer over 2 years ago

Would need to accept array of those too (whether its a method or symbol) for things like:

private attr_accessor :foo

#9 Updated by Alexey Muranov over 2 years ago

Excuse me Thomas, what's the point in a "private attribute accessor"?

#10 Updated by Thomas Sawyer over 2 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?

#11 Updated by Koichi Sasada over 2 years ago

  • Assignee set to Yusuke Endoh

mame-san, could you judge this ticket?

IMO, it is too slow to introduce such a big feature.

#12 Updated by Yusuke Endoh over 2 years ago

  • Status changed from Open to Assigned
  • Target version changed from 2.0.0 to Next Major
  • Assignee changed from Yusuke Endoh to Yukihiro Matsumoto

IMO, it is too slow to introduce such a big feature.

Completely agreed.

Yusuke Endoh mame@tsg.ne.jp

#13 Updated by Alexey Muranov over 2 years ago

Please do not forget also module_function and such. (Or are they different in some sense?).

Alexey.

#14 Updated by Nobuyoshi Nakada 3 months ago

  • Description updated (diff)

Also available in: Atom PDF