From: Daniel Carrera Date: 2003-07-10T15:26:14+09:00 Subject: Re: Ruby T-Shirt Idea -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 On Thu, Jul 10, 2003 at 03:18:01PM +0900, Mauricio Fern�ndez wrote: > > Did you see mine? It was one line! (with a semicolon though :) > > They're the same if you omit the fact that I forgot about Enumerable#grep ;) > It turns out that $<, $DEFAULT_INPUT _and_ ARGF are synonyms. I didn't know that. I should have guessed. > IMHO one semicolon is equivalent to one line-break but we can still make > it a one-liner without cheating: > > re = ARGV.shift and puts ARGF.grep(/#{re}/) > > However, I don't quite like this example, it feels perlish. > I'd rather show singleton methods at work, or blocks, or metaclasses... Okay, so we agree on: re = ARGV.shift; ARGF.each{|x| puts x if x =~ /#{re}/} It's Rubyish, and it avoids the perlishms like "$<" (akward variable names) and the "and". A slight modification would be: re = ARGV.shift; puts ARGF.map{|x| x if x =~ /#{re}/}.compact I'm not sure which one I like better. - -- Daniel Carrera | OpenPGP fingerprint: Graduate TA, Math Dept | 6643 8C8B 3522 66CB D16C D779 2FDD 7DAC 9AF7 7A88 UMD (301) 405-5137 | http://www.math.umd.edu/~dcarrera/pgp.html -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.2.2 (SunOS) iD8DBQE/DQb/nxE8DWHf+OcRArbCAKCO/B1vLUvv4Ld8zkoDxaNO3AN5rQCggZXc /PJ7o97NU+GHXaIqYm0DeKY= =aeFu -----END PGP SIGNATURE-----