Question about symbol naming convention

Hello,
I'm glad to get so well founded help here, so here's another beginner
question:
Trying to make a singleton method private I tried this:

class MyClass
  def MyClass.class_private
  end

  def private
  end

  private :private
  private :MyClass::class_private
end

The nomenclature of the singleton method doesn't make it.
What's the right naming?

Thanks in advance
Stefan Achatz

Stefan Achatz said:

Hello,
I'm glad to get so well founded help here, so here's another beginner
question:
Trying to make a singleton method private I tried this:

class MyClass
  def MyClass.class_private
  end

  def private
  end

  private :private
  private :MyClass::class_private
end

The nomenclature of the singleton method doesn't make it.
What's the right naming?

irb(main):001:0> class MyClass
irb(main):002:1> def self.bar
irb(main):003:2> p 'bar'
irb(main):004:2> end
irb(main):005:1> end
=> nil
irb(main):006:0> MyClass.bar
"bar"
=> nil
irb(main):007:0> class MyClass
irb(main):008:1> class << self
irb(main):009:2> private :bar
irb(main):010:2> end
irb(main):011:1> end
=> #<Class:MyClass>
irb(main):012:0> MyClass.bar
NoMethodError: private method `bar' called for MyClass:Class
        from (irb):12
irb(main):013:0>

So to summarize the above, you need to go inside the singleton class for
MyClass to make any singleton methods private.

Ryan

Ryan Leavengood wrote:

Stefan Achatz said:

Hello,
I'm glad to get so well founded help here, so here's another beginner
question:
Trying to make a singleton method private I tried this:

class MyClass
  def MyClass.class_private
  end

  def private
  end

  private :private
  private :MyClass::class_private
end

The nomenclature of the singleton method doesn't make it.
What's the right naming?

irb(main):001:0> class MyClass
irb(main):002:1> def self.bar
irb(main):003:2> p 'bar'
irb(main):004:2> end
irb(main):005:1> end
=> nil
irb(main):006:0> MyClass.bar
"bar"
=> nil
irb(main):007:0> class MyClass
irb(main):008:1> class << self
irb(main):009:2> private :bar
irb(main):010:2> end
irb(main):011:1> end
=> #<Class:MyClass>
irb(main):012:0> MyClass.bar
NoMethodError: private method `bar' called for MyClass:Class
        from (irb):12
irb(main):013:0>

So to summarize the above, you need to go inside the singleton class for
MyClass to make any singleton methods private.

Ryan

It's hard to understand, why i can't access the private singleton from
Myclass anymore:

class Myclass
def testing
self.bar
end
def self.bar
end
class << self
private :bar
end
end

I should say that i'm coming from c++ and it seems im lacking some
understanding in the differences between static methods in c++ and
singleton methods in ruby.
Stefan

They're not at all related. They look like they might be, but
they're not.

By making Myclass.bar private, you are making it so that it can only
be called within its *self*, which is Myclass[1]. Therefore,
instances of Myclass cannot call Myclass.bar because their *self* is
not Myclass, but an instance of Myclass.

-austin
[1] Modulo, of course, #__send__.

···

On 8/4/05, Stefan Achatz <erazor@mitochondrien.de> wrote:

Ryan Leavengood wrote:

Stefan Achatz said:

It's hard to understand, why i can't access the private singleton
from Myclass anymore:
class Myclass
  def testing
    self.bar
  end
  def self.bar
  end
  class << self
    private :bar
  end
end

I should say that i'm coming from c++ and it seems im lacking some
understanding in the differences between static methods in c++ and
singleton methods in ruby.

--
Austin Ziegler * halostatue@gmail.com
               * Alternate: austin@halostatue.ca

Austin Ziegler wrote:

> Ryan Leavengood wrote:
>> Stefan Achatz said:
> It's hard to understand, why i can't access the private singleton
> from Myclass anymore:
> class Myclass
> def testing
> self.bar
> end
> def self.bar
> end
> class << self
> private :bar
> end
> end

> I should say that i'm coming from c++ and it seems im lacking some
> understanding in the differences between static methods in c++ and
> singleton methods in ruby.

They're not at all related. They look like they might be, but
they're not.

By making Myclass.bar private, you are making it so that it can only
be called within its *self*, which is Myclass[1]. Therefore,
instances of Myclass cannot call Myclass.bar because their *self* is
not Myclass, but an instance of Myclass.

To be more explicit, there are two reasons that won't work:

1. The method #testing is an instance method. You can't call class
methods as "self.whatever" in an instance method. It would be
"self.class.whatever".

2. The meaning of "private" in Ruby is "cannot be called with an
explicit receiver." That is, you would have to call "self.bar" as just
"bar" in your test method.

···

On 8/4/05, Stefan Achatz <erazor@mitochondrien.de> wrote:

"Charles Steinman" <acharlieblue@gmail.com> writes:

2. The meaning of "private" in Ruby is "cannot be called
with an explicit receiver."

No, it's "cannot be called with an explicit receiver unless
the method name ends with an equals sign and the explicit
receiver is the four characters `self'."

By the way, why not simplify that to "cannot be called with
an explicit receiver other than the four characters `self'"?

···

--
Daniel Brockman <daniel@brockman.se>