# Once more unto the breach: private class methods

**URL:** <https://www.ruby-forum.com/t/once-more-unto-the-breach-private-class-methods/112854>\
**Category:** Ruby\
**Created:** [August 23, 2007, 10:10pm UTC](https://www.ruby-forum.com/t/once-more-unto-the-breach-private-class-methods/112854 "2007-08-23T22:10:57Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Zeekar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/a8b319/32.png) [@Zeekar](https://www.ruby-forum.com/u/Zeekar)\
**Post date:** [August 23, 2007, 10:10pm UTC](https://www.ruby-forum.com/t/once-more-unto-the-breach-private-class-methods/112854/1 "2007-08-23T22:10:57Z")

</div>

Found a lot of discussion of this, but it didn’t seem to suggest a  
solution to a common pattern. The below completely useless example is  
modeled after a longer and more useful one in the Agile Web  
Development with Rails book (2nd ed):

class Foo  
class \<\< self  
private  
def staticHelperFunction  
12345  
end  
end  
public  
def initialize  
@var = self.class.staticHelperFunction  
end  
end  
Foo.new.var

Now, the above won’t work, obviously. If I replace the direct call to  
staticHelperFunction with instance\_eval or send, it will work, but  
that feels hackish. Is there an idiomatic Ruby way to handle this  
sort of situation?

In case the goal isn’t clear, I want instance (not singleton/class)  
methods of a given class to have access to what are essentially helper  
functions/subroutines (not other instance methods of the same class)  
which are not generally accessible outside of that class.

Doable?

---

<div class="post-metadata">

**Author:** ![Zeekar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/a8b319/32.png) [@Zeekar](https://www.ruby-forum.com/u/Zeekar)\
**Post date:** [August 23, 2007, 11:03pm UTC](https://www.ruby-forum.com/t/once-more-unto-the-breach-private-class-methods/112854/2 "2007-08-23T23:03:04Z")

</div>

Hi –

On Fri, 24 Aug 2007, Zeekar wrote:

> ```
> end
> 
> ```
> 
> end  
> public  
> def initialize  
> @var = self.class.staticHelperFunction  
> end  
> end  
> Foo.new.var
> 
> Now, the above won’t work, obviously. If I replace the direct call to  
> staticHelperFunction with instance\_eval or send, it will work,

Not until you write a ‘var’ instance method 🙂

> but that feels hackish. Is there an idiomatic Ruby way to handle  
> this sort of situation?
> 
> In case the goal isn’t clear, I want instance (not singleton/class)  
> methods of a given class to have access to what are essentially helper  
> functions/subroutines (not other instance methods of the same class)  
> which are not generally accessible outside of that class.

It seems like you’re threading a very narrow needle. Do you mean that  
you would not want class methods to see each other?

class C  
def self.x # assume this is the kind of method you mean  
end

```
 def self.y
   x # This would fail?
 end

```

end

That’s a pretty tall order: having methods from which you can’t call  
other singleton methods of the very same object. Or am I  
misunderstanding?

David

---

<div class="post-metadata">

**Author:** ![Zeekar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/a8b319/32.png) [@Zeekar](https://www.ruby-forum.com/u/Zeekar)\
**Post date:** [August 23, 2007, 11:12pm UTC](https://www.ruby-forum.com/t/once-more-unto-the-breach-private-class-methods/112854/3 "2007-08-23T23:12:04Z")

</div>

On 8/23/07, Zeekar [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> end
> 
> ```
> 
> that feels hackish. Is there an idiomatic Ruby way to handle this  
> sort of situation?
> 
> In case the goal isn’t clear, I want instance (not singleton/class)  
> methods of a given class to have access to what are essentially helper  
> functions/subroutines (not other instance methods of the same class)  
> which are not generally accessible outside of that class.
> 
> Doable?  
> Sure, but clumsy, I do not know if it is worth it

class Foo  
class \<\< self  
private  
def x; 60 end  
end  
attr\_reader :x  
helper = method(:x)  
define\_method :initialize do @x = helper.call end  
end

puts Foo.new.x

HTH  
Robert

---

<div class="post-metadata">

**Author:** ![Zeekar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/a8b319/32.png) [@Zeekar](https://www.ruby-forum.com/u/Zeekar)\
**Post date:** [August 23, 2007, 11:26pm UTC](https://www.ruby-forum.com/t/once-more-unto-the-breach-private-class-methods/112854/4 "2007-08-23T23:26:07Z")

</div>

On Aug 23, 5:02 pm, “David A. Black” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> It seems like you’re threading a very narrow needle. Do you mean that  
> you would not want class methods to see each other?

Not at all. I want to define a method C.foo() that other classes  
outside of C can’t call directly. Other class methods of C can call  
it, fine. But the goal is to have _instances_ of C to be able to call  
it.

I guess I could make it a private instance method that just happens  
not to care about self., but that seems like misleading design.

---

<div class="post-metadata">

**Author:** ![Zeekar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/a8b319/32.png) [@Zeekar](https://www.ruby-forum.com/u/Zeekar)\
**Post date:** [August 23, 2007, 11:28pm UTC](https://www.ruby-forum.com/t/once-more-unto-the-breach-private-class-methods/112854/5 "2007-08-23T23:28:07Z")

</div>

On Aug 23, 5:11 pm, “Robert D.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> end  
> Well, that is an interesting approach. Thanks! It might be in the  
> same category as using instance\_eval or send, but at first blush it  
> feels somewhat cleaner to me…

---

<div class="post-metadata">

**Author:** ![Zeekar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/a8b319/32.png) [@Zeekar](https://www.ruby-forum.com/u/Zeekar)\
**Post date:** [August 23, 2007, 11:56pm UTC](https://www.ruby-forum.com/t/once-more-unto-the-breach-private-class-methods/112854/6 "2007-08-23T23:56:11Z")

</div>

Hi –

On Fri, 24 Aug 2007, Zeekar wrote:

> On Aug 23, 5:02 pm, “David A. Black” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > It seems like you’re threading a very narrow needle. Do you mean that  
> > you would not want class methods to see each other?
> 
> Not at all. I want to define a method C.foo() that other classes  
> outside of C can’t call directly. Other class methods of C can call  
> it, fine. But the goal is to have _instances_ of C to be able to call  
> it.

OK – I didn’t get that; there was something to the effect that you  
wanted instance methods, but not singleton or class methods, to be  
able to call these methods, which is what confused me.

> I guess I could make it a private instance method that just happens  
> not to care about self., but that seems like misleading design.

The code I came up with is similar to Ara’s, except that I didn’t make  
the instance method version private which I believe probably does make  
sense.

David

---

<div class="post-metadata">

**Author:** ![Zeekar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/a8b319/32.png) [@Zeekar](https://www.ruby-forum.com/u/Zeekar)\
**Post date:** [August 23, 2007, 11:35pm UTC](https://www.ruby-forum.com/t/once-more-unto-the-breach-private-class-methods/112854/7 "2007-08-23T23:35:09Z")

</div>

On Aug 23, 2007, at 2:10 PM, Zeekar wrote:

> In case the goal isn’t clear, I want instance (not singleton/class)  
> methods of a given class to have access to what are essentially helper  
> functions/subroutines (not other instance methods of the same class)  
> which are not generally accessible outside of that class.

easy:

cfp:~ \> cat a.rb  
class C  
singleton\_class =  
class \<\< self  
self  
end

helper = Object.new.instance\_eval do  
def foo() 42 end  
self  
end

[self, singleton\_class].each do |c|  
c.module\_eval{  
define\_method(:helper){ helper }  
private :helper  
}  
end

def initialize  
@foo = helper.foo  
end  
end

p C.new  
C.helper

cfp:~ \> ruby a.rb  
#\<C:0x24b94 @foo=42\>  
a.rb:25: private method `helper’ called for C:Class (NoMethodError)

but in the presence of both instance\_eval, eval, and send - why bother?

a @ [http://drawohara.com/](http://drawohara.com/)

---

<div class="post-metadata">

**Author:** ![Zeekar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/a8b319/32.png) [@Zeekar](https://www.ruby-forum.com/u/Zeekar)\
**Post date:** [August 24, 2007, 12:21am UTC](https://www.ruby-forum.com/t/once-more-unto-the-breach-private-class-methods/112854/8 "2007-08-24T00:21:13Z")

</div>

On Aug 23, 2:20 pm, Zeekar [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I want to define a method C.foo() that other classes  
> outside of C can’t call directly. Other class methods of C can call  
> it, fine. But the goal is to have _instances_ of C to be able to call  
> it.
> 
> I guess I could make it a private instance method that just happens  
> not to care about self., but that seems like misleading design.

Without seeing the code you’re actually working with, it seems to me  
that I’d much rather see the method implemented as a private instance  
method than as a class method (assuming that the purpose is to serve  
as a utility method for use inside instances of the class).

I’m not sure why that would be considered misleading, and it would be /  
much/ easier to follow the code than if you try to use some of the  
other solutions offered. Not that the code suggested is /bad/, it’s  
just overkill for what you seem to be trying to accomplish, IMO.

–  
Regards,

John W.
