Skip to content

Instantly share code, notes, and snippets.

@joefiorini
Created June 27, 2011 15:23
Show Gist options
  • Save joefiorini/1049083 to your computer and use it in GitHub Desktop.
Save joefiorini/1049083 to your computer and use it in GitHub Desktop.
Indent methods under private?
class Blah
private
def method1
end
def method2
end
end
# vs.
class Blah
private
def method1
end
def method2
end
end
# vs.
class Blah
def public_method
end
private
def method1
end
def method2
end
end
@seanhussey
Copy link

I go with #2, but stay consistent with pre-existing codebases. I've also made use of private :method_name (which @brianvh pointed out). I can't be sure, but I think I got that from something Dave Thomas once said, maybe in the PragProg Metaprogramming screencasts.

@mindscratch
Copy link

I do what @george does

@jeltz
Copy link

jeltz commented Jul 16, 2011

#2 makes the most sense since it is just a method invocation, while #3 is slightly less readable, and #1 I have never liked.

@tokland
Copy link

tokland commented Dec 15, 2012

I use #3 although I #2 is certaintly more accurate since "public/protected/private" are not special keywords but normal method calls. #1 makes me feel sick, indentation levels are too precious a thing to waste them like that.

@kotp
Copy link

kotp commented Jun 2, 2013

class Classy
  def initialize
  end

  private
  begin
    def private_method_1
    end

    def private_method_2
    end
  end
end

Someone wrote private do which wouldn't work, but got me thinking about the above example.

@hindenbug
Copy link

#2

@rafaltrojanowski
Copy link

#1 - according to Rails convention. Vim support this with vim-ruby and in vimrc: let g:ruby_indent_access_modifier_style="indent"

@joelhelbling
Copy link

@shved
Copy link

shved commented Jul 24, 2016

Indenting private section is good for huge classes where you can find some private method, trying to use it, and then realise that it is actually private. So I use #2 for small classes and #1 for huge classes with a lot of private methods.
#3 way seems very strange and meaningless.

@jedeleh
Copy link

jedeleh commented Jul 31, 2017

I've always used 1 but at my current job they use 2 so I've switched to that for the sake of consistency. I don't feel strongly but prefer 1.

However, I really dislike 3 as it visually interrupts the scope of the class.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment