From: Robert Klemme Date: 2007-01-28T02:26:06+09:00 Subject: Re: a Hash and a Set (sitting in a tree) On 27.01.2007 18:00, Shot (Piotr Szotkowski) wrote: > Robert Klemme: > >> Well, but with a block you get clear >> semantics at the price of a low overhead. > > The catch was, I considered even just a 10% overhead to be worth > engineering the classes in a simpler, if less-obvious way. First, > though, I figured that if test-driven Ruby development was so easy > to learn, maybe benchmarking in Ruby is also easy, and I came with > these: http://shot.pl/blankets/ Btw, you can more easily create your Arrays of randoms with the block form: irb(main):001:0> Array.new(5) { rand 8 } => [0, 5, 6, 6, 6] Much less typing. :-) > Now that I thought of it, Block could simply be a subclass of Set with > an additional instance variable of @encoding. [Googles a bit, finds > a way for a class to call it�s parent�s constructor, rejoices.] > Something to the point of > > class Block < Set > def initialize enum = [], encoding = nil > super enum > @encoding = encoding > end > end > > Any pitfalls a Ruby newbie should be aware of with such an approach? Hm... It seems generally considered that delegation is preferred over inheritance with basic classes like Set. You have to be at least aware that you inherit *all* methods of Set - this might or might not be what you want. In this case I cannot see a Set method that you might not want to inherit but one should be aware of this. > Thanks a lot for your time and your > answers, Robert; I really appreciate it! You're welcome! Kind regards robert