can you rubyfy this?

B

Boris Schulz

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
 
J

Jeff Pritchard

Boris said:
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
 
D

dblack

Hi --

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 :)
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 :) the
code in the standard library.


David

--
David A. Black | (e-mail address removed)
Author of "Ruby for Rails" [1] | Ruby/Rails training & consultancy [3]
DABlog (DAB's Weblog) [2] | Co-director, Ruby Central, Inc. [4]
[1] http://www.manning.com/black | [3] http://www.rubypowerandlight.com
[2] http://dablog.rubypal.com | [4] http://www.rubycentral.org
 
D

David Vallner

--------------enigD446A2E8B75398E94827C994
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

Jeff said:
Boris said:
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 =3D node.elements["LOCATION"].attributes["DIR"] +
node.elements["LOCATION"].attributes["FILE"]
if File.exists?(file)
tag =3D Tag.new(file)
if !node.attributes["ARTIST"]
node.add_element "ARTIST"
end
node.attributes["ARTIST"] =3D tag.artist
end
}
out =3D File.new("test.xml", "w+")
doc.write out
=20
Hi Boris,
Main thing would be to replace the entrys.each { } construct with
entrys.each do |node|
stuff in here
end
=20
Multi-line {} blocks are legal but not the "expected" way. The rest of= =20
it looks ok to me.
=20

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 =3D
node.elements.to_a('location/dir/node() | location/file/node()') out of
sheer sadism to readers. Yay XPath golf.

David Vallner


--------------enigD446A2E8B75398E94827C994
Content-Type: application/pgp-signature; name="signature.asc"
Content-Description: OpenPGP digital signature
Content-Disposition: attachment; filename="signature.asc"

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.5 (MingW32)

iD8DBQFFUToyy6MhrS8astoRAu+jAJ92o4hePyTMUqt9rO8Iq7LLV9FoVwCfUWx6
YVZLD8KMitTcBZsCaAGToDM=
=5kUH
-----END PGP SIGNATURE-----

--------------enigD446A2E8B75398E94827C994--
 
T

Trans

Boris said:
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.
 
D

dblack

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

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

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

--
David A. Black | (e-mail address removed)
Author of "Ruby for Rails" [1] | Ruby/Rails training & consultancy [3]
DABlog (DAB's Weblog) [2] | Co-director, Ruby Central, Inc. [4]
[1] http://www.manning.com/black | [3] http://www.rubypowerandlight.com
[2] http://dablog.rubypal.com | [4] http://www.rubycentral.org
 
C

ChrisH

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
 
J

Jeff Pritchard

ChrisH said:
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
 
S

spooq

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 :)
 
B

Boris Schulz

Hi all,

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

greets, B.
 
D

David Vallner

--------------enigD8E9E9C066E53F1B7D689F82
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

Jeff said:
Use of do/end=20
obviates the need for the usual tug of war on this age-old issue.
=20

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


--------------enigD8E9E9C066E53F1B7D689F82
Content-Type: application/pgp-signature; name="signature.asc"
Content-Description: OpenPGP digital signature
Content-Disposition: attachment; filename="signature.asc"

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.5 (MingW32)

iD8DBQFFUls5y6MhrS8astoRAlpNAJ0dWw0mEIQnOGgc+Xr406dUjcMWeACfUPe5
popwOQi2bFOVCkNoWI8tGy4=
=H8k0
-----END PGP SIGNATURE-----

--------------enigD8E9E9C066E53F1B7D689F82--
 
D

David Vallner

--------------enig5E8929945E1039B06338609E
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

Robert said:
I will make this short, and there is no way to discuss this, it is a G= o?od
given law
=20
"Discussion of syntax style is the single most important issue in the
Universe."
=20

O Noes!
BTW David what do you mean by the four letter word starting with w and
ending with ork?
=20

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

David Vallner


--------------enig5E8929945E1039B06338609E
Content-Type: application/pgp-signature; name="signature.asc"
Content-Description: OpenPGP digital signature
Content-Disposition: attachment; filename="signature.asc"

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.5 (MingW32)

iD8DBQFFU5ZJy6MhrS8astoRAv5KAJ4/rdiknCzgAN5REev6MWlS4ra5SACfa3BJ
Kf3J3qi7loV+vNsqB3OiLvQ=
=1m8a
-----END PGP SIGNATURE-----

--------------enig5E8929945E1039B06338609E--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,218
Messages
2,571,124
Members
47,728
Latest member
SusanGsc94

Latest Threads

Top