Hi Pat,

On 7/01/2016 4:43 PM, Pat Allan wrote:
Further to this:

I know it seems counter-intuitive, but the private keyword (at least,
when used as a keyword rather than a method) doesn't have any impact on
class-level methods. As far as I know, this has always been the way Ruby
behaves.

I thought it was like this too, until I was reading through the `money` gem source recently...

Here's an example (rubocop clean) of `private` working on a class method when defined on the eigenclass, and then an example of it failing when using def self.methodname.

Can someone explain Julius Sumner Miller, or "why is it so?"

# This is properly private
class Eigen
  class << self
    # def caller; priv; end

    private

    def priv
      puts 'I\'m private in Eigen'
    end
  end
end

# Here, the `not_priv` method is *not* private - why?
class Self
  # def self.caller; not_priv; priv; end # both calls work just fine

  private

  def self.not_priv
    puts 'I am NOT private in Self'
  end

  private_class_method def self.priv
    puts 'Im AM private in Self'
  end
end

Self.not_priv # WORKS!?!? why not private??

# As expected: private method `priv' called for Eigen:Class (NoMethodError)
Eigen.priv

# As expected: private method `priv' called for Self:Class (NoMethodError)
Self.priv

--
You received this message because you are subscribed to the Google Groups "Ruby or 
Rails Oceania" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/rails-oceania.
For more options, visit https://groups.google.com/d/optout.

Reply via email to