A newbie would like some code criticism, please...

  • Thread starter Just Another Victim of the Ambient Morality
  • Start date
J

Just Another Victim of the Ambient Morality

I'm learning Ruby and I'd like some criticism on a program that I wrote.
It interfaces with iTunes and runs through all the songs, (reasonably)
ensuring proper title capitalization. It's a small program and I would have
simply posted except that, for some unknown reason, MS Outlook Express keeps
converting the tabs into single spaces.
I'm open to comments on style versus what is standard protocol for the
Ruby community, as well.
I hope the program may be as useful to any of you as it is for me...

http://theorem.ca/~dlkong/iTunes_title_filter.rb.gz


There's also a pattern that often comes up for me and I would like some
help with it...

first = true
list.each do |item|
if first
# do something special for the first case

first = false
else
# do something else for every other item
end
end

Is there a better way to do this? Perhaps some way to simply iterate
over every element other than the first one? Something that avoid a check
every iteration?

Thank you...
 
D

Daniel Schierbeck

Just said:
first = true
list.each do |item|
if first
# do something special for the first case

first = false
else
# do something else for every other item
end
end

Is there a better way to do this? Perhaps some way to simply iterate
over every element other than the first one? Something that avoid a check
every iteration?

There are several ways of doing this:

list.each_with_index do |item, i|
if i == 0
# first item
else
# not the first item
end
end

Though it's not very pretty


# returns the first item and removes it
# from the array
list.shift

list.each do |item|
# the rest of the items
end

Note that this alters `list'! The following won't:

# returns the first item, keeps it in
# the array
list.first

ary[1...ary.length].each do |item|
# all but the first item
end

I'm sure there are even more ways of doing it.


Cheers,
Daniel
 
J

Jon Evans

Hi,
There's also a pattern that often comes up for me and I would like some
help with it...

first = true
list.each do |item|
if first
# do something special for the first case

first = false
else
# do something else for every other item
end
end

Is there a better way to do this? Perhaps some way to simply iterate
over every element other than the first one? Something that avoid a check
every iteration?

If you don't mind clobbering 'list':

do_something_for_first(list.shift)
list.each do |item|
do_something_else(item)
end

or if you can't alter list, how about:

do_something_for_first(list[0])
list[1..-1].each do |item| ...

When faced with problems like that I find it handy to fire up irb and
just try different things until the solution emerges. It also helps to
have the pages for 'Enumerable' and 'Array' bookmarked in Programming
Ruby.

Jon
 
R

Robert Klemme

Just said:
I'm learning Ruby and I'd like some criticism on a program that I wrote.
It interfaces with iTunes and runs through all the songs, (reasonably)
ensuring proper title capitalization. It's a small program and I would have
simply posted except that, for some unknown reason, MS Outlook Express keeps
converting the tabs into single spaces.
I'm open to comments on style versus what is standard protocol for the
Ruby community, as well.
I hope the program may be as useful to any of you as it is for me...

http://theorem.ca/~dlkong/iTunes_title_filter.rb.gz


There's also a pattern that often comes up for me and I would like some
help with it...

first = true
list.each do |item|
if first
# do something special for the first case

first = false
else
# do something else for every other item
end
end

Is there a better way to do this? Perhaps some way to simply iterate
over every element other than the first one? Something that avoid a check
every iteration?

Sometimes also #inject will help but that depends on what you do in the
block. For example:

irb(main):001:0> %w{aa bb cc dd}.inject {|a, b| a + ", " + b}
=> "aa, bb, cc, dd"

Other than that I'd probably stick with the solution that uses
#each_with_index because it at least avoids creating a new array.

Kind regards

robert
 
J

Just Another Victim of the Ambient Morality

Robert Klemme said:
Sometimes also #inject will help but that depends on what you do in the
block. For example:

irb(main):001:0> %w{aa bb cc dd}.inject {|a, b| a + ", " + b}
=> "aa, bb, cc, dd"

Other than that I'd probably stick with the solution that uses
#each_with_index because it at least avoids creating a new array.

inject skips over the first element! This is exactly what I'm looking
for!
Thank you...
 
P

Patrick Spence

Tim said:
The error is:
C:\tools\scripts\test>ruby itunes.rb
itunes.rb:4:in `ole_methods': Failed to GetTypeInfo (RuntimeError)
HRESULT error code:0x8002801d
Library not registered. from itunes.rb:4

To chime in with the other responses, it sounds like the iTunes.exe, or
whatever it's called, is not registered as automation server on your
machine. You might try logging into the directory where the executable
is installed, and running the following command line:

iTunes.exe /regserver
 
D

dblack

Hi --

Honestly I do not think so, personally I prefer do..end to {..} unless {..}
looks prettier.
I too see a tendency to use {} on short blocks but would not go so far to
say that e.g.

File.open(__FILE__)' {
|f|
f.each {
...
}
}

is bad or even uncommon practice.

I think putting |f| on its own line is uncommon, and kind of
odd-looking, though it will certainly work.


David

--
http://www.rubypowerandlight.com => Ruby/Rails training & consultancy
http://www.manning.com/black => RUBY FOR RAILS (reviewed on
Slashdot, 7/12/2006!)
http://dablog.rubypal.com => D[avid ]A[. ]B[lack's][ Web]log
(e-mail address removed) => me
 

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
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top