Can you rubyfy this?

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

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

Hi –

On Wed, 8 Nov 2006, 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?

greets, B.


#!/usr/bin/ruby
require “rexml/document”;

Lose the semi-colons :slight_smile:

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”] +

Indentation is normally/traditionally two spaces.

You can get a good sense of traditional style from (most of :slight_smile: the
code in the standard library.

David

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.

Jeff P. wrote:

 if File.exists?(file)

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 V.

On Wed, 8 Nov 2006, David V. wrote:

Jeff P. wrote:

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

Arriviste :slight_smile:

I got curious about this, so here are some very rough-and-ready stats.

First, Ruby 1.8.2. $rfiles is all the .rb files in the standard lib.

{|| } blocks

$ grep “{ ?|” $rfiles | wc -l
1729

{|| } blocks on multiple lines

$ grep “{ ?|” $rfiles | grep -v “}” | wc -l
724

do blocks

$ grep " do " $rfiles | wc -l
1130

do blocks on multiple lines

$ grep " do " $rfiles | grep -v “end” | wc -l
1093

Here’s the same for 1.8.5:

Braces: 1121
Braces on multiple lines: 379
do/end: 1275
do/end on multiple lines: 1237

Summary:

Block use in general is down. Or my grepping is even cruder than it
looks :slight_smile:

do/end has gained some popularity, relative to all blocks (53% of all
blocks vs. 39%).

do/end is, and was, more common for multi-line blocks.

David

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

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 O. True Brace Style :wink:

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 :slight_smile:

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

Jeff P. 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 V.

On 11/8/06, David V. [email protected] wrote:

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 V.

I will make this short, and there is no way to discuss this, it is a
Go?od
given law

“Discussion of syntax style is the single most important issue in the
Universe.”

BTW David what do you mean by the four letter word starting with w and
ending with ork?

:slight_smile:


The reasonable man adapts himself to the world; the unreasonable one
persists in trying to adapt the world to himself. Therefore all progress
depends on the unreasonable man.

  • George Bernard Shaw

Robert D. wrote:

I will make this short, and there is no way to discuss this, it is a Go?od
given law

“Discussion of syntax style is the single most important issue in the
Universe.”

O Noes!

BTW David what do you mean by the four letter word starting with w and
ending with ork?

It’s that place where you read through Daily WTF backlogs without paying
for the broadband and goad less important and headstrong people into
making you money! Duh :stuck_out_tongue:

David V.

On 11/9/06, David V. [email protected] wrote:

O Noes!

BTW David what do you mean by the four letter word starting with w and
ending with ork?

It’s that place where you read through Daily WTF backlogs without paying
for the broadband and goad less important and headstrong people into
making you money! Duh :stuck_out_tongue:

David V.

Ah just a synonym for fun, I was just worried :wink:


The reasonable man adapts himself to the world; the unreasonable one
persists in trying to adapt the world to himself. Therefore all progress
depends on the unreasonable man.

  • George Bernard Shaw

Hi all,

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

greets, B.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs