views:

161

answers:

2

I'm new to Ruby (experienced with Python, C++ and C). I need to create a class that is only to be used by other classes and methods in a module. In Python, I'd just call it __classname. I'd use an empty typedef in C++. How do I do this in Ruby (or am I barking up the wrong tree and not doing this the "Ruby way"?)

+1  A: 

I haven't seen such concept so far in Ruby, but I guess you could simulate that by creating private method which would return a class created as a local variable (remember that in Ruby, a class is an object just like any other, and can be instantiated in a method and returned by it).

BTW, even private methods in Ruby aren't as private as in other languages - you can always access them using send method. But doing that implies you know what you are doing.

Mladen Jablanović
Damn. I figured it was possible, but I hoped it wouldn't be hackish... Oh well. Guess I'll find a more Ruby-ish way to do it. Thanks though - you answered my question.
c4757p
You should wait for other answers, I may as well be wrong. BTW, what's that you want to achieve with private classes? Oh, another thing: instance vars are always _private_ in Ruby, and in order to expose them, you need to create accessor methods, but no one considers that _hackish_. :)
Mladen Jablanović
Actually I think doing that /is/ a bit hackish - it's one of the things I don't like about Ruby. The other is the stupid implicit function call - I really wish I could just refer to a function by name without calling it, without using a symbol or lambda.I'm dynamically interfacing with an external library using FFI, and I wanted to keep the FFI C 'struct's away from the usable module.
c4757p
IMHO, adding a language construct for a feature which can be easily covered by existing ones (such as `attr_accessor` method) would be hackish. I appreciate Ruby very much for that kind of simplicity, although I would remove even more keywords (like completely excess `for`). And regarding function calls, I agree with you there. BTW, I am pretty sure one could "wrap" making class private into a single method call pretty easy, so at least it shouldn't _look_ hackish. :)
Mladen Jablanović
+3  A: 
Jörg W Mittag
"We are all consenting adults" is a slogan from the Python community, not the Ruby community.
Andrew Grimm
I didn't mean to imply that it belongs to the Ruby community, only that it is used there. But you are right, the wording is misleading. Twenty years of learning English apparently still isn't enough :-)
Jörg W Mittag