What's the Ruby Way to hide class helpers?

Start with:

class C
  def self.class_callme
    internal_helper
  end

  def instance_callme
    C.internal_helper
  end

  def self.internal_helper
    puts "I'm not needed by the outside world."
  end
end

My first instinct was to make internal_helper protected:

class << self
  protected
    def internal_helper
      puts "I'm not needed by the outside world."
    end
end

However, instance_methods (such as instance_callme) can't call protected
class methods.

Is there some other idiomatic Ruby way to, er, protect internal_helper, or
another way to call it from instance_callme? Or do I just have to do it
through lack of documentation with :nodoc:, as Rails seems to do?

Jay Levitt

Jay Levitt wrote:

Start with:

class C
  def self.class_callme
    internal_helper
  end

  def instance_callme
    C.internal_helper
  end

  def self.internal_helper
    puts "I'm not needed by the outside world."
  end
end

My first instinct was to make internal_helper protected:

class << self
  protected
    def internal_helper
      puts "I'm not needed by the outside world."
    end
end

However, instance_methods (such as instance_callme) can't call protected
class methods.

Is there some other idiomatic Ruby way to, er, protect internal_helper, or
another way to call it from instance_callme? Or do I just have to do it
through lack of documentation with :nodoc:, as Rails seems to do?

Jay Levitt

Why not just stick internal_helper in a nested module? (Or does internal_helper need to access class state?)

class C
   def self.class_callme
     M.internal_helper
   end

   def instance_callme
     M.internal_helper
   end

   module M
     def self.internal_helper
       puts "I'm not needed by the outside world."
     end
   end
end

C.new.instance_callme # ==> I'm not needed by the outside world.

···

--
       vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

That works for me in the current case; internal_helper routines don't need
any class state (and, in general, I try to avoid having any such state,
rebelling against my C days of static variables).

Out of curiosity, though, if I did need my internal_helper routine to get
at state, what would you recommend? Seems like the only solution in Ruby
is to extract the internals out to another object that's only called by my
public, interface object - but that seems so un-Rubyish.

Jay

···

On Fri, 25 Aug 2006 12:53:44 +0900, Joel VanderWerf wrote:

Why not just stick internal_helper in a nested module? (Or does
internal_helper need to access class state?)