Sam Sungshik Kong wrote:
I'm getting more and more confused...
Before I started Ruby, I thought I understood OOP concepts very clearly
(with Java, C#, even Python).
But Ruby puts me in a deep confusion with singleton methods, somewhat
different concept of private/protected accessibility, metaclass, etc...
Java and (to the best of my knowledge) C# have a particular philosophy of OO that differs from Ruby (and probably Python).
My understanding of OO tends to center on the notion of sending messages to receivers; the receiver may or may not know how to handle the message. The receiver may have originated from a class derived from one or more other classes, but, once created, may have acquire new or altered behavior not found in any of the base class(es). Messages sent, and methods implemented in the receiver, should not have to map to one another; how an object responds to a message is up to that particular object. The default behavior for handling unknown messages tends to be an exception, but that should be left to the object (i.e., the developer), not hard-coded into the language.
By analogy, think of a web site. Pages generally map to specific URLs. Many web sites simply toss back a 500 or 404 when given an unknown request, but the smarter sites know how to map all URLs to something useful. Now imagine if a web site was operationally prevented from handling unknown URLs. Or if a user could not even *try* to request a page without knowing the full, correct URL in advance. (The statically-typed Web?)
This view of OO runs counter to statically typed languages such as Java and C#, where behavior tends to be set in stone early on, and object behavior is defined by method invocation rather than message handling.
The idea that one object may be able to respond to messages that another object of the same class cannot is alien in Java, but normal in Ruby.
Likewise, the idea that classes and objects are malleable during runtime is largely foreign in Java, but an essential fact of Ruby.
I'm not sure how much any of this helps you with your actual problem :); I'm not even sure how accurate it is*. Getting your head around Ruby is may be an adventure if you're coming from Java/C#.
There's also a good take on what constitutes various flavors of OO on Paul Graham's site [0]. (There's lot's of other good stuff on his site, BTW. I particularly like his comments on Arc and OO [1])
James
* I'm trusting somebody here will promptly correct me where I'm wrong. But no flamewars, please.
[0] Rees Re: OO
[1] Why Arc Isn't Especially Object-Oriented