From: dblack@... Date: 2005-12-10T05:25:32+09:00 Subject: Re: new to Ruby - pls help in translating this --8323328-218870398-1134159926=:15796 Content-Type: MULTIPART/MIXED; BOUNDARY="8323328-218870398-1134159926=:15796" This message is in MIME format. The first part should be readable text, while the remaining parts are likely unreadable without MIME-aware tools. --8323328-218870398-1134159926=:15796 Content-Type: TEXT/PLAIN; charset=X-UNKNOWN; format=flowed Content-Transfer-Encoding: QUOTED-PRINTABLE Hi -- On Sat, 10 Dec 2005, Simon Kr=F6ger wrote: > >> I'll go with: >>=20 >> seen =3D {} >>=20 >> ARGF.each do |elem| >> print elem if seen.include? elem >> seen[elem] =3D true >> end > > Thanks, this is how ruby should look like! > > here is another one (i like it a bit more > functional style): > > quantities =3D Hash.new{|h, k| h[k]=3D0} > ARGF.each{|l| quantities[l.chomp.to_i] +=3D 1} Since you're doing a h[x] +=3D 1 thing, you don't need to do the auto-assignment in the block: quantities =3D Hash.new(0) will work fine. Also, String#to_i is smart enough not to need the chomp. So, as so often happens, code evaporates from the screen as one works on a Ruby program :-) David --=20 David A. Black dblack@wobblini.net "Ruby for Rails", forthcoming from Manning Publications, April 2006! --8323328-218870398-1134159926=:15796-- --8323328-218870398-1134159926=:15796--