# Module private methods?

**URL:** <https://www.ruby-forum.com/t/module-private-methods/79922>\
**Category:** Ruby\
**Created:** [December 20, 2006, 6:21pm UTC](https://www.ruby-forum.com/t/module-private-methods/79922 "2006-12-20T18:21:40Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![splattael](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/858c86/32.png) [@splattael](https://www.ruby-forum.com/u/splattael)\
**Post date:** [December 20, 2006, 6:21pm UTC](https://www.ruby-forum.com/t/module-private-methods/79922/1 "2006-12-20T18:21:40Z")

</div>

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](http://www.rubyrailways.com)

---

<div class="post-metadata">

**Author:** ![splattael](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/858c86/32.png) [@splattael](https://www.ruby-forum.com/u/splattael)\
**Post date:** [December 20, 2006, 7:27pm UTC](https://www.ruby-forum.com/t/module-private-methods/79922/2 "2006-12-20T19:27:00Z")

</div>

Peter S. wrote:

> ```
> baz()
> 
> ```
> 
> 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.
