[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

module private methods?

Peter Szinek

12/19/2006 8:46:00 PM

Hello,

I guess the subject is silly - but since I need something like Java's
"package private" I have used a similar name.

What I would need is an additional access modifier, something like this:

Module Foo
class Bar
#public methods
baz()
fluff()
module_private
ork()
private
crap()
end
end

baz() and fluff() are public as normally, crap() is private as normally.
ork() is "module private" which means it is visible to the caller only
if the caller is *also* in the module Foo - otherwise not.

Can this behavior achieved somehow easily?

TIA,
Peter

__
http://www.rubyra...




1 Answer

Trans

12/20/2006

0


Peter Szinek wrote:
> Hello,
>
> I guess the subject is silly - but since I need something like Java's
> "package private" I have used a similar name.
>
> What I would need is an additional access modifier, something like this:
>
> Module Foo
> class Bar
> #public methods
> baz()
> fluff()
> module_private
> ork()
> private
> crap()
> end
> end
>
> baz() and fluff() are public as normally, crap() is private as normally.
> ork() is "module private" which means it is visible to the caller only
> if the caller is *also* in the module Foo - otherwise not.
>
> Can this behavior achieved somehow easily?

"local methods". they've been discussed before, so for more details you
can do a search for them. but ruby doesn't support them.

I'm don't think it can be achieved from the "defining end" (w/o
changien ruby itself) but something like it is possible from the
calling end by using Facets' as(Foo).ork, eg.
foo.instance_method(:ork).bind(self).call. Alternatively you could
define a local lambda, but to call it you have to use define_method:

ork = lambda { ...

define_method(:foo) do
ork[]
end

if state is not an issue (eg instance variable access) then you can
always make a module method:

def self.ork
...

hth,
t.