[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

can you rubyfy this?

Boris Schulz

11/7/2006 7:51:00 PM

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?

greets, B.
- - - - - - - - - - -
#!/usr/bin/ruby
require "rexml/document";
require "id3lib";
include REXML;
include ID3Lib;

doc = Document.new File.new("collection.nml")

entrys = doc.elements.to_a("//ENTRY")
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
11 Answers

Jeff Pritchard

11/8/2006 1:42:00 AM

0

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.

jp

--
Posted via http://www.ruby-....

dblack

11/8/2006 1:55:00 AM

0

David Vallner

11/8/2006 2:01:00 AM

0

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

Trans

11/8/2006 2:11:00 AM

0


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?

Looks good. Just some small touchups. One thing to point out: Some
prefer to use call paraenteticals as little as possible. Other's, like
myself, prefer to use them in most cases.

#!/usr/bin/ruby

require "rexml/document"
require "id3lib"

include REXML
include ID3Lib

doc = Document.new(File.new("collection.nml"))

entrys = doc.elements.to_a("//ENTRY")
entrys.each do |node|
loc = node.elements["LOCATION"]
file = loc.attributes["DIR"] + loc.attributes["FILE"]
if File.exist?(file) # no plural
tag = Tag.new(file)
unless node.attributes["ARTIST"]
node.add_element "ARTIST"
end
node.attributes["ARTIST"] = tag.artist
end
end

out = File.new("test.xml", "w+")
doc.write(out)

T.

dblack

11/8/2006 2:27:00 AM

0

Chris Hulan

11/8/2006 3:03:00 PM

0

I'm no expert, but this is how I would do it:

#!/usr/bin/ruby
require "rexml/document"
require "id3lib"
include REXML
include ID3Lib

File.new("collection.nml", 'r'){|inF|
doc = Document.new(inF)

doc.root.each_element("//ENTRY"){|node|
#use File.join to ensure path is good and cross-platform
file = File.join(node.elements["LOCATION"].attributes["DIR"],
node.elements["LOCATION"].attributes["FILE"])
if File.exists?(file)
#use 'unless' instead of 'if !'
node.add_attribute "ARTIST" unless node.attributes["ARTIST"]
#why assign Tag to a variable if only uses one?
node.attributes["ARTIST"] = Tag.new(file).artist
end
}

File.new("test.xml", "w+"){|out|
#use block to ensure file is closed,
doc.write out
}
}


Cheers
Chris

Jeff Pritchard

11/8/2006 4:04:00 PM

0

ChrisH wrote:
> I'm no expert, but this is how I would do it:
>
> #!/usr/bin/ruby
> require "rexml/document"
> require "id3lib"
> include REXML
> include ID3Lib
>
> File.new("collection.nml", 'r'){|inF|
> doc = Document.new(inF)
>
> doc.root.each_element("//ENTRY"){|node|
> #use File.join to ensure path is good and cross-platform
> file = File.join(node.elements["LOCATION"].attributes["DIR"],
> node.elements["LOCATION"].attributes["FILE"])
> if File.exists?(file)
> #use 'unless' instead of 'if !'
> node.add_attribute "ARTIST" unless node.attributes["ARTIST"]
> #why assign Tag to a variable if only uses one?
> node.attributes["ARTIST"] = Tag.new(file).artist
> end
> }
>
> File.new("test.xml", "w+"){|out|
> #use block to ensure file is closed,
> doc.write out
> }
> }
>
>
> Cheers
> Chris


I'm no expert either, but I'm going to have to ask for a "Man Law" from
the group on this one. No use of the uglier of the two types of brace
alignment that came out of the 'C' world. I know it's just a matter of
taste, and I mean no disrespect to Chris or his opinion, but I for one
just can't stomach this style of brace alignment. Seeing it in a Ruby
script is, for me, like fingernails on a blackboard. Use of do/end
obviates the need for the usual tug of war on this age-old issue.

jp


--
Posted via http://www.ruby-....

Luke Graham

11/8/2006 4:40:00 PM

0

> Use of do/end
> obviates the need for the usual tug of war on this age-old issue.

Or simply adds another upstart rival for The One True Brace Style ;)

> No use of the uglier of the two types of brace
> alignment that came out of the 'C' world.

Obviously I came out of that horrible place also :)

Boris Schulz

11/8/2006 7:49:00 PM

0

Hi all,

thanks a lot for your answers! This should help me in doing stuff the
ruby way :-)

greets, B.

David Vallner

11/8/2006 10:34:00 PM

0

Jeff Pritchard wrote:
> Use of do/end
> obviates the need for the usual tug of war on this age-old issue.
>

Wax earplugs, shaking head in disbelief, and throwing foam bricks at
anyone who thinks spending as much as half a second on discussing brace
alignment styles (read: wasting said time of mine griping about it
pointlessly) works as good for me.

Java has official guidelines, C# has official guidelines, Ruby's style
is enforced by the parser, Python completely circumvents the issue
altogether by not using brackets as block delimiters. That pretty much
settles the issue in all programming languages I am likely to regularly
use, and anyone trying to argue against official guidelines with me is
summarily told to learn to use the revolutionary feature that is syntax
highlighting and code browsers like the rest of us and get back to work.

David Vallner