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?
On Sat, Jul 12, 2008 at 3:49 AM, عمر ملقب بالثانی [email protected] wrote:
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 to both of you ara.t.howard and Kentaro-san for the nice hints.
As far as I could tell from your tips, Ruby-style encourages conciseness
(hence the use of hash.update or the custom .set method that returns the
hash). And it seems that inject({}) is in any case preferred to h={}
and each loop, right?
GOTO Kentaro wrote:
Do you like this?
[…]
Yes. Short and to the point but still quite clear to a beginner.
ara.t.howard wrote:
i prefer
[…]
I liked the map method a lot (it reminds me the Perl map) and the way
one can build the hash step by step with .update (or .merge!) [on that,
I checked in The Ruby P.ming Language]. I’ll try to stick on that
kind of idiom.
. use space to give meaning: a newline is one char
. use explicit var names as comments
I am already used to this with Perl… (I pasted a slightly less spaced
code here)
. try to minimize method chaining so stacktraces to line #42 have
at least some meaning
I got your point on minimising method chaining but not what you meant
with line #42.
simply that an error message in the logs like
foobar exception from foo.rb:42
…
is very hard to debug when the line looks like this
foo.bar.foobar.barfoo.method
On Jul 11, 2008, at 12:49 PM, عمر ملقب بالثانی wrote:
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
href = entry.search("h4/a").first['href']
list = entry.search("div/ul/li/a").map{|tag| tag.inner_html}
hash.update href => list
end
. use space to give meaning: a newline is one char
. use explicit var names as comments
. try to minimize method chaining so stacktraces to line #42 have
at least some meaning
. use hash.update with inject because it returns self
these things are religious i realize but, coming from perl, you
understand.
Surely the aim of all programming is to pack as much functionality as
possible into a single line of code. Everyone knows newlines are
horrendously expensive, so they should be used sparingly.
Surely the aim of all programming is to pack as much functionality as
possible into a single line of code. Everyone knows newlines are
horrendously expensive, so they should be used sparingly.
I’ve found that my instantiating only a single newline and passing
around a reference has helped my coding style immensely!