From: fedzor Date: 2008-01-18T05:59:07+09:00 Subject: Re: Properly using Ruby's specialness On Jan 17, 2008, at 2:04 PM, J. Cooper wrote: > I was recently reading a presentation on "10 things Java programmers > should know about Ruby." Like many similar discourses, it talked about > classes being objects and singleton methods etc. etc. Now, I am > familiar > with these different things, but I think I am too stupid to discern > their inherent worth. > > 1. Open classes. The other day, in a little Blackjack game I wrote in > Ruby, I used this feature to add in a method to the Array class > instead > of making a class that inherited from Array and added that method. > However, a) I'm not sure if that was the "right" way and b) I'm not > sure > if there is an advantage to that approach over the latter, per se. That was the correct way to do it! The advantage is you can instantiate an array object like normal ([]), instead of having to do MyArray.new(3, 4, 5) > 2. Singleton methods. I really can't think of a particular example > where > I would use this... please help! When you would need to add a method into a class, or create a subclass with a special method BUT the method is only used by one object (IE, only one instantiation of MyArray). Also, you should use it when you want methods to be called within a class but outside of a method definition. > 3. Blocks. Although now (after some practice and getting used to) > these > make sense to me when I use them with methods already designed, I > don't > think I have internalized them to the point where I would write my own > methods that accepted them and worked with them. Has anyone else > been in > the same boat, or do they usually just "click"? They typically just click :-)