From: Guillaume Cottenceau Date: 2001-08-07T02:02:06+09:00 Subject: [ruby-talk:19235] Re: ANN: Code Amelioration Contest (presented by Ruby Conference 2001) Dave Thomas writes: [...] > > > Anyway, I propose that instead of improving arbitary code, the contest > > > should aim to improve cookbook code. > > If Guillaume is amenable, then I think that's a great idea. Of course, I think this is great! Again, I just want to underline that in some cases, I may refuse the "improvement" if I feel that it obfuscates the code, considering intended readers mostly not Ruby Gurus; of course, much of existing code probably deserves a look from a Ruby specialist. [...] > > For example, in @@PLEAC@@_1.6 we have the following: > > > > irb(main):009:0> long_palindromes = IO.readlines("/usr/share/dict/words"). > > select { |w| w.chomp!; w.reverse == w && w.length > 5 } > > ["redder", "reviver", "rotator"] > > > > > > This could be considered as "better" than using File.open("..").readlines > > because there is one method call less, and it doesn't get obfuscated. > > Now, I'm not saying that this is an improvement, but 'readlines' reads the > whole file in before processing. Could we make use of the fact that IO > is Enumerable? > > File.open(/usr/share/dict/words").select { ... } > > Yup - that runs about 30% faster on my box. And is still very clear. I'm patching with that, thanks ;p. > I'm thinking that part of the Amelioration process is trying to come > up with a 'Ruby style', which to me involves the use of some of the > more functional-style idioms. The fact that File is Enumerable is > actually a very neat thing in Ruby. Yep. (to me, Ruby style also implies use of iterators; also, use of OO "pointed" notation rather than what's you can read in Perl in the Cookbook, e.g. obj.collect {} <=> map {} obj) -- Guillaume Cottenceau - http://people.mandrakesoft.com/~gc/