In article <d287614.0211241749.63173bf7 / posting.google.com>, adamon / mailandnews.com (Damon) wrote: > Ross Shaw <rshaw1961 / yahoo.com.au> wrote in message news:<rshaw1961-> Robert > Cowham <rc / vaccaperna.co.uk> wrote: > > > > > What's the best ruby idiom for the following Perl: > > > > > > open(EMAIL, "<$EMAIL_FILE") or die "Failed to open $EMAIL_FILE"; > > > my %hash = map {chomp; split /\=/} (<EMAIL>); > > > close (EMAIL); > > > > > > (the above reads a file containing text like > > > > > > key1=value1 > > > key2=value2 > > > > > > into a hash. > > > > > > So far I have: > > > > > > h = {} > > > f = File.open("fred.ini", "r") > > > f.each_line{|l| s = l.chomp.split("="); h[s[0]] = s[1]} > > > f.close > > > > > > > > > > > > > h = {} > > File.foreach("fred.ini") do |line| > > key, value = line.chomp.split("=") > > h[key] = value > > end > > > > > > Ross > > > Thank you! This is clear and still concise. The other responses seem > to gravitate towards Perl-like one-liners. But this defeats the > purpose of writing in Ruby---to avoid Perl obfuscations. The above is > much more readable and also much more idiomatic than many of the > others. This should go into athe Ruby Cookbook (if it were still up). I have to come clean. I only did this implementation because I could not for the life of me figure out how to get Hash update working with an array (as per many of the other solutions). I looked up my Pickaxe and Ruby in a Nutshell and still could not work it out. So it was only dumb ignorance that left me with what I had. But on reflection and given your appreciation I am happier with what I did put up. Ross