[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

What's the Ruby Way to hide class helpers?

Jay Levitt

8/25/2006 2:21:00 AM

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
4 Answers

Joel VanderWerf

8/25/2006 3:54:00 AM

0

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

Jay Levitt

8/25/2006 12:20:00 PM

0

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?)

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

Bill Williams

6/15/2012 12:50:00 PM

0

Thanks for pointing us to this video.
The LK solo act with audience participation segueing into Now’s the Time offers a classic live moment. Great to hear Ted Dunbar in this context, too.

BW


van

6/15/2012 7:36:00 PM

0

On Friday, June 15, 2012 8:19:38 AM UTC-4, Bill Williams wrote:
> Isn't Lee doing a Gene Wilder impersonation, here?

LOL!!