From: "عمر ملقب بالثانی" Date: 2008-07-12T03:49:06+09:00 Subject: inject idiom Dear all, I am new to Ruby (coming right away from Perl). I've been playing with Hpricot for the sake of learning and wrote a simple script to hash link entries with their tags from del.icio.us front page. My first attempt is more or less Perl-inspired: require 'hpricot' require 'open-uri' doc = Hpricot(open("http://del.icio.us/")) tags = {} doc.search("//div.hotlist/ol/li").each do |entry| link = entry.search("h4/a").first['href'] taglist = [] entry.search("div/ul/li/a").each do |tag| taglist << tag.inner_html end tags[link] = taglist end p tags I then tried to use the inject method: doc = Hpricot( open("http://del.icio.us/") ) tags = doc.search("//div.hotlist/ol/li").inject({}) do | hash, entry | link = entry.search("h4/a").first['href'] hash[link] = entry.search("div/ul/li/a").inject([]) do | list, tag | list << tag.inner_html end hash end I wonder what the best practice is? The most readable and best suited to Ruby-style of programming. In Perl, although there is more than one way to do every thing you want, there are definitely best practices. What about Ruby? How do one use the inject idiom best? Thanks in advance to all Rubyists for their help, Ömer. -- Posted via http://www.ruby-forum.com/.