module private methods?

P

Peter Szinek

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.rubyrailways.com
 
T

Trans

Peter said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,705
Latest member
Stefkari24

Latest Threads

Top