How does the private method work?

Hello,

I just saw one developer do this in his code :

private if some_condition?
  def first_method
     end

I was wondering how the private method works. How does it figure out which
methods to mark private ?

It would be nice if someone could explain this or point me to appropriate
resources on the web.

- bitsapien

https://en.m.wikibooks.org/wiki/Ruby_Programming/Syntax/Classes#Private
seems to explain the usage well.

···

On Tue, 17 Apr 2018, 12:46 bit sapien, <bitsapien@gmail.com> wrote:

Hello,

I just saw one developer do this in his code :

private if some_condition?
  def first_method
     end

I was wondering how the private method works. How does it figure out
which methods to mark private ?

It would be nice if someone could explain this or point me to appropriate
resources on the web.

- bitsapien

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt;

As I understand, bitsapien is searching for a technical explanation of the private method, not a syntactical one.

How does it technically work that it marks all subsequent methods as private.

···

Am 17.04.2018 um 09:42 schrieb Paul Kuruvilla <rohitpaulk@gmail.com>:

Ruby Programming/Syntax/Classes - Wikibooks, open books for an open world seems to explain the usage well.

On Tue, 17 Apr 2018, 12:46 bit sapien, <bitsapien@gmail.com <mailto:bitsapien@gmail.com>> wrote:
Hello,

I just saw one developer do this in his code :

private if some_condition?
  def first_method
     end

I was wondering how the private method works. How does it figure out which methods to mark private ?

It would be nice if someone could explain this or point me to appropriate resources on the web.

- bitsapien

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org <mailto:ruby-talk-request@ruby-lang.org>?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt;

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt;

Re: How does the private method work?
bit,

The idea behind “private” in most/all OOP languages is that you are telling other developers and/or yourself to not use this/these methods in the future as

  1. the interface to the method may change

  2. the side effects may change

When you declare a method, e.g.

def some_method(arg1, arg2, arg3)

#Do something

#return another_thing

end

you are establishing a “contract” between your object and the outside world.

By declaring the method private you are telling the world “Don’t call this! I may not implement this functionality at all in future versions!”

Ralph

Tuesday, April 17, 2018, 1:16:08 AM, you wrote:

Hello,

I just saw one developer do this in his code :

private if some_condition?

def first_method

 end

I was wondering how the private method works. How does it figure out which methods to mark private ?

It would be nice if someone could explain this or point me to appropriate resources on the web.

  • bitsapien

Ralph

Thanks Ralph. I understand what "private" as an OOP construct does. But as
Christian mentioned, I am looking at how ruby interprets it. For instance,
I see ruby also allows this

  def first_method
  end

  def second_method
  end

  private :first_method, :second_method

Is the above private and the private I mentioned in my initial question,
the same ? If not, how are they not the same ?

···

On Tue, Apr 17, 2018 at 1:27 PM, Ralph Shnelvar <ralphs@dos32.com> wrote:

bit,

The idea behind "private" in most/all OOP languages is that you are
telling other developers and/or yourself to not use this/these methods in
the future as
1) the interface to the method may change
2) the side effects may change

When you declare a method, e.g.

def some_method(arg1, arg2, arg3)
#Do something
#return another_thing
end

you are establishing a "contract" between your object and the outside
world.

By declaring the method private you are telling the world "Don't call
this! I may not implement this functionality at all in future versions!"

Ralph

Tuesday, April 17, 2018, 1:16:08 AM, you wrote:

Hello,

I just saw one developer do this in his code :

private if some_condition?
def first_method
    end

I was wondering how the private method works. How does it figure out which
methods to mark private ?

It would be nice if someone could explain this or point me to appropriate
resources on the web.

- bitsapien

Ralph

Re: How does the private method work?
bit,

I’m not quite sure what you are asking but …

“private” is not a keyword but, instead, a method. So one can dynamically make other methods, uh, private or not.

Two links of interest might be

https://medium.com/@tjoye20/ruby-access-control-basics-public-vs-private-vs-protected-methods-7788b26e04a7

https://gnuu.org/2011/01/01/rubys-private-keyword-is-not-heresy/

Ralph

Tuesday, April 17, 2018, 3:21:32 AM, you wrote:

Thanks Ralph. I understand what “private” as an OOP construct does. But as Christian mentioned, I am looking at how ruby interprets it. For instance, I see ruby also allows this

def first_method

end

def second_method

end

private :first_method, :second_method

Is the above private and the private I mentioned in my initial question, the same ? If not, how are they not the same ?

···

On Tue, Apr 17, 2018 at 1:27 PM, Ralph Shnelvar ralphs@dos32.com wrote:

bit,

The idea behind “private” in most/all OOP languages is that you are telling other developers and/or yourself to not use this/these methods in the future as

  1. the interface to the method may change

  2. the side effects may change

When you declare a method, e.g.

def some_method(arg1, arg2, arg3)

#Do something

#return another_thing

end

you are establishing a “contract” between your object and the outside world.

By declaring the method private you are telling the world “Don’t call this! I may not implement this functionality at all in future versions!”

Ralph

Tuesday, April 17, 2018, 1:16:08 AM, you wrote:

Hello,

I just saw one developer do this in his code :

private if some_condition?

def first_method

end

I was wondering how the private method works. How does it figure out which methods to mark private ?

It would be nice if someone could explain this or point me to appropriate resources on the web.

  • bitsapien

Ralph

Ralph

Private has two forms:

1. private without arguments — sets flag "all methods defined below are
private"
2. private with arguments accepts arguments-Symbols and makes methods with
those names private.

This form:

def meth
end

private :meth

Is absolutely equivalent to this:

private def meth
end

That is because def *expression* returns the name of defined method.

You can check it this way:

p def meth
...
end

Next, this form:

private if something
  def meth1
  end
else
  def meth2
  end
end

...is, again the same:
* if expression returns what calculated in the branch performed,
* which is result of def expression,
* which is name of the method defined,
* which is passed to private, which makes method private.

Hope this helps.

···

2018-04-17 12:21 GMT+03:00 bit sapien <bitsapien@gmail.com>:

Thanks Ralph. I understand what "private" as an OOP construct does. But as
Christian mentioned, I am looking at how ruby interprets it. For instance,
I see ruby also allows this

  def first_method
  end

  def second_method
  end

  private :first_method, :second_method

Is the above private and the private I mentioned in my initial question,
the same ? If not, how are they not the same ?

On Tue, Apr 17, 2018 at 1:27 PM, Ralph Shnelvar <ralphs@dos32.com> wrote:

bit,

The idea behind "private" in most/all OOP languages is that you are
telling other developers and/or yourself to not use this/these methods in
the future as
1) the interface to the method may change
2) the side effects may change

When you declare a method, e.g.

def some_method(arg1, arg2, arg3)
#Do something
#return another_thing
end

you are establishing a "contract" between your object and the outside
world.

By declaring the method private you are telling the world "Don't call
this! I may not implement this functionality at all in future versions!"

Ralph

Tuesday, April 17, 2018, 1:16:08 AM, you wrote:

Hello,

I just saw one developer do this in his code :

private if some_condition?
def first_method
    end

I was wondering how the private method works. How does it figure out
which methods to mark private ?

It would be nice if someone could explain this or point me to appropriate
resources on the web.

- bitsapien

Ralph

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt;

Hi,
private is instance method of Class class.

[3] pry(main)> show-source Class#private

Owner: Module
Visibility: private
Number of lines: 5

static VALUE
rb_mod_private(int argc, VALUE *argv, VALUE module)
{
    return set_visibility(argc, argv, module, METHOD_VISI_PRIVATE);
}

so private in class is just calling the method.

def A
  private if condition # just call method A#private
  def hoge
  end
def

It is interesting to note that private method itself is private.

[11] pry(main)> Class.new.private
NoMethodError: private method `private' called for #<Class:0x007fb460eeb1c0>

thanks,

···

From: vm_method.c (C Method):

2018-04-17 22:28 GMT+09:00 Victor Shepelev <zverok.offline@gmail.com>:

Private has two forms: Class: Module (Ruby 2.5.0)
i-private

1. private without arguments — sets flag "all methods defined below are
private"
2. private with arguments accepts arguments-Symbols and makes methods with
those names private.

This form:

def meth
end

private :meth

Is absolutely equivalent to this:

private def meth
end

That is because def *expression* returns the name of defined method.

You can check it this way:

p def meth
...
end

Next, this form:

private if something
  def meth1
  end
else
  def meth2
  end
end

...is, again the same:
* if expression returns what calculated in the branch performed,
* which is result of def expression,
* which is name of the method defined,
* which is passed to private, which makes method private.

Hope this helps.

2018-04-17 12:21 GMT+03:00 bit sapien <bitsapien@gmail.com>:

Thanks Ralph. I understand what "private" as an OOP construct does. But
as Christian mentioned, I am looking at how ruby interprets it. For
instance, I see ruby also allows this

  def first_method
  end

  def second_method
  end

  private :first_method, :second_method

Is the above private and the private I mentioned in my initial question,
the same ? If not, how are they not the same ?

On Tue, Apr 17, 2018 at 1:27 PM, Ralph Shnelvar <ralphs@dos32.com> wrote:

bit,

The idea behind "private" in most/all OOP languages is that you are
telling other developers and/or yourself to not use this/these methods in
the future as
1) the interface to the method may change
2) the side effects may change

When you declare a method, e.g.

def some_method(arg1, arg2, arg3)
#Do something
#return another_thing
end

you are establishing a "contract" between your object and the outside
world.

By declaring the method private you are telling the world "Don't call
this! I may not implement this functionality at all in future versions!"

Ralph

Tuesday, April 17, 2018, 1:16:08 AM, you wrote:

Hello,

I just saw one developer do this in his code :

private if some_condition?
def first_method
    end

I was wondering how the private method works. How does it figure out
which methods to mark private ?

It would be nice if someone could explain this or point me to
appropriate resources on the web.

- bitsapien

Ralph

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt;

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt;

Thanks matsumotokazuya7, zverok.offline, and Ralph Shnelvar for helping
out. All that explanation and resources helped.

···

On Tue, Apr 17, 2018 at 7:34 PM, 松本和也 <matsumotokazuya7@gmail.com> wrote:

Hi,
private is instance method of Class class.

[3] pry(main)> show-source Class#private

From: vm_method.c (C Method):
Owner: Module
Visibility: private
Number of lines: 5

static VALUE
rb_mod_private(int argc, VALUE *argv, VALUE module)
{
    return set_visibility(argc, argv, module, METHOD_VISI_PRIVATE);
}

so private in class is just calling the method.

def A
  private if condition # just call method A#private
  def hoge
  end
def

It is interesting to note that private method itself is private.

[11] pry(main)> Class.new.private
NoMethodError: private method `private' called for
#<Class:0x007fb460eeb1c0>

thanks,

2018-04-17 22:28 GMT+09:00 Victor Shepelev <zverok.offline@gmail.com>:

Private has two forms: https://ruby-doc.org/co
re-2.5.0/Module.html#method-i-private

1. private without arguments — sets flag "all methods defined below are
private"
2. private with arguments accepts arguments-Symbols and makes methods
with those names private.

This form:

def meth
end

private :meth

Is absolutely equivalent to this:

private def meth
end

That is because def *expression* returns the name of defined method.

You can check it this way:

p def meth
...
end

Next, this form:

private if something
  def meth1
  end
else
  def meth2
  end
end

...is, again the same:
* if expression returns what calculated in the branch performed,
* which is result of def expression,
* which is name of the method defined,
* which is passed to private, which makes method private.

Hope this helps.

2018-04-17 12:21 GMT+03:00 bit sapien <bitsapien@gmail.com>:

Thanks Ralph. I understand what "private" as an OOP construct does. But
as Christian mentioned, I am looking at how ruby interprets it. For
instance, I see ruby also allows this

  def first_method
  end

  def second_method
  end

  private :first_method, :second_method

Is the above private and the private I mentioned in my initial
question, the same ? If not, how are they not the same ?

On Tue, Apr 17, 2018 at 1:27 PM, Ralph Shnelvar <ralphs@dos32.com> >>> wrote:

bit,

The idea behind "private" in most/all OOP languages is that you are
telling other developers and/or yourself to not use this/these methods in
the future as
1) the interface to the method may change
2) the side effects may change

When you declare a method, e.g.

def some_method(arg1, arg2, arg3)
#Do something
#return another_thing
end

you are establishing a "contract" between your object and the outside
world.

By declaring the method private you are telling the world "Don't call
this! I may not implement this functionality at all in future versions!"

Ralph

Tuesday, April 17, 2018, 1:16:08 AM, you wrote:

Hello,

I just saw one developer do this in his code :

private if some_condition?
def first_method
    end

I was wondering how the private method works. How does it figure out
which methods to mark private ?

It would be nice if someone could explain this or point me to
appropriate resources on the web.

- bitsapien

Ralph

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org
?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt;

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt;

Unsubscribe: <mailto:ruby-talk-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk&gt;

Note, there is a subtlety:

$ irb
Ruby version 2.3.1
irb(main):001:0> class A; private def f;1;end; def g;2;end end
=> :g
irb(main):002:0> o = A.new
=> #<A:0x000000026cbf68>
irb(main):003:0> o.f
NoMethodError: private method `f' called for #<A:0x000000026cbf68>
        from (irb):3
        from /usr/bin/irb:11:in `<main>'
irb(main):004:0> o.g
=> 2
irb(main):005:0> class B; private; def f;1;end; def g;2;end end
=> :g
irb(main):006:0> o = B.new
=> #<B:0x0000000264fd00>
irb(main):007:0> o.f
NoMethodError: private method `f' called for #<B:0x0000000264fd00>
        from (irb):7
        from /usr/bin/irb:11:in `<main>'
irb(main):008:0> o.g
NoMethodError: private method `g' called for #<B:0x0000000264fd00>
        from (irb):8
        from /usr/bin/irb:11:in `<main>'

This is not new - Victor explained it. I just wanted to make the point
that the two cases of invoking Module#private without arguments or
with at least one argument can be easily confused. Of course, normally
one writes the no argument form on a separate line:

class X
  private

  def this_method_will_be_private
  end

  def this_method_will_be_private_too
  end
end

Cheers

robert

···

On Wed, Apr 18, 2018 at 12:46 PM, bit sapien <bitsapien@gmail.com> wrote:

Thanks matsumotokazuya7, zverok.offline, and Ralph Shnelvar for helping out. All that explanation and resources helped.

--
[guy, jim, charlie].each {|him| remember.him do |as, often| as.you_can
- without end}
http://blog.rubybestpractices.com/