From: William James Date: 2005-12-13T03:32:49+09:00 Subject: Re: new to Ruby - pls help in translating this Eric Hodel wrote: > On Dec 11, 2005, at 7:27 AM, William James wrote: > > > William James wrote: > >> William James wrote: > >>> Eric Hodel wrote: > >>> > >>>> seen = {} > >>>> > >>>> ARGF.each do |elem| > >>>> print elem if seen.include? elem > >>>> seen[elem] = true > >>>> end > >>> > >>> seen = {} > >>> while s = gets > >>> print s if seen.key? s > >>> seen[ s ] = nil > >>> end > >>> > >>> Or: > >>> > >>> seen = Hash.new(0) > >>> while s = gets > >>> print s if ( seen[s] += 1 ) > 1 > >>> end > > This is starting to get pointlessly obfuscated. > > >> seen, s = Hash.new(0) > >> print s if ( seen[s] += 1 ) > 1 while s = gets > > Stop. You're making things hard to read for the new people. > > > seen = Hash.new(0) > > print if ( seen[$_] += 1 ) > 1 while gets > > I can't read that so I don't know how you expect someone new to Ruby > to read it. A standard Ruby idiom: instead of 3 lines . . . if test print "ok" end .. . . 1 line: print "ok" if test Also, as in Awk, "print" with no argument prints the line just read. Very simple. If one needs to explicitly refer to the line just read, one uses "$_". And "while gets" is simpler and clearer than "ARGF.each do |elem|".