David Vallner
11/8/2006 2:01:00 AM
Jeff Pritchard wrote:
> Boris Schulz wrote:
>> Hi,
>>
>> I often read something about code being "rubystyle" though I am not
>> exactly sure what it means. Maybe someone can help me out and put the
>> couple of lines below into a typical ruby style?
>> ...
>> entrys.each{|node|
>> file = node.elements["LOCATION"].attributes["DIR"] +
>> node.elements["LOCATION"].attributes["FILE"]
>> if File.exists?(file)
>> tag = Tag.new(file)
>> if !node.attributes["ARTIST"]
>> node.add_element "ARTIST"
>> end
>> node.attributes["ARTIST"] = tag.artist
>> end
>> }
>> out = File.new("test.xml", "w+")
>> doc.write out
>
> Hi Boris,
> Main thing would be to replace the entrys.each { } construct with
> entrys.each do |node|
> stuff in here
> end
>
> Multi-line {} blocks are legal but not the "expected" way. The rest of
> it looks ok to me.
>
I claim subject to fashion / taste - I can't recall do/end being used
much for blocks at all when I joined the list in the wild, wild days of
1.8.2 *duck*
As for the OP's question, I'd parenthesise all method calls, spell
"entries" correctly, and probably use a statement modifier unless
instead of the one-line "if not".
And personally, I'd also use file =
node.elements.to_a('location/dir/node() | location/file/node()') out of
sheer sadism to readers. Yay XPath golf.
David Vallner