5

Within a Ruby class definition, what is the scopes of the private keyword in the following scenarios:

class Foo

  def bar_public
    puts "public"
  end

private
  def bar_private
    puts "private"
  end

  def bar_public_2
    puts "another public"
  end

end

Does private only act on bar_private? or on bar_public_2 as well?

Andrew Grimm
  • 78,473
  • 57
  • 200
  • 338
pseudosudo
  • 1,962
  • 1
  • 19
  • 30

2 Answers2

8

In your case both bar_private and bar_public_2 are private.

That is because both methods are "within scope" of the private keyword.

> f = Foo.new
#<Foo:0xf1c770>
> Foo.new.bar_private
NoMethodError: private method 'bar_private' called for #<Foo:0xf1c770>
> Foo.new.bar_public_2
NoMethodError: private method 'bar_public_2' called for #<Foo:0xf1c770>

Either way, the best way to answer you question is to open IRB and try it out ;-)

Jeremy
  • 22,188
  • 4
  • 68
  • 81
  • @Ed: "Just try it" is a great way to get tangled up in accidental and unspecified behavior. Programming through experimentation is generally a bad idea. – mu is too short Jun 30 '11 at 18:34
  • 2
    @mu `%s/Programming/Designing` -- Programming is *all about* experimentation. – Jeremy Jun 30 '11 at 18:37
  • 3
    @mu is too short: I don't mean this to be rude, but that is complete nonsense. To be good in our field it is *required* that one has a sense of experimentation and curiosity. If you can't be bothered to run a few tests and experiment a bit then you probably aren't very good at your job. That doesn't mean you should forego the documentation, but this question would have easily been answered with a trivial test. Please explain to me how that test could have possibly led the OP down the road of *"[getting] tangled up in accidental and unspecified behavior"*. – Ed S. Jun 30 '11 at 18:39
  • @Ed: My comment is more about the general "programming by experimentation" behavior. I have seen a lot of horrific garbage produced by people using the "fiddle with it until it works" approach rather than the "understand what is happening and why" approach. I think you're reading too much into a single sentence. – mu is too short Jun 30 '11 at 19:44
  • @Ed: And [here's a good example](http://stackoverflow.com/questions/6539882/my-app-cannot-load-some-images-from-the-server/6540118) of what happens when you program by experimentation rather than understanding what's going on. – mu is too short Jun 30 '11 at 19:48
  • 1
    @mu: I think we all have a misunderstanding here. What @Ed and I are getting at is that if you have a question that can be answered by pulling up an IRB session and playing around, then that's a **good** thing to do. No one will agree that playing around with application code like you've just described is a good thing. But that's not experimentation, that's just throwing sh*t at the wall and seeing what sticks. Experimentation is guided by your ambition to *actually* learn something. – Jeremy Jun 30 '11 at 19:56
  • 1
    @Jeremy, @Ed: Right, you're arguing about one thing and I'm arguing about another so we're all just shouting at the walls. That's the way most arguments happen :) – mu is too short Jul 01 '11 at 03:25
4

If you find it weird that private is affecting both bar_private and bar_public_2, then rather than use private, use private :bar_private after defining bar_private.

Andrew Grimm
  • 78,473
  • 57
  • 200
  • 338