[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

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

Just Another Victim of the Ambient Morality

7/5/2006 10:40:00 PM

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


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


7 Answers

Daniel Schierbeck

7/5/2006 11:13:00 PM

0

Just Another Victim of the Ambient Morality wrote:
> 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

evans.jon@gmail.com

7/5/2006 11:22:00 PM

0

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

Robert Klemme

7/6/2006 8:17:00 AM

0

Just Another Victim of the Ambient Morality wrote:
> 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_fi...
>
>
> 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

Tim Hoolihan

7/6/2006 3:05:00 PM

0

I'm trying to test the itunes code but can't get it to work?
Even the following script errors out:

#test itunes with ole
require 'win32ole'

itunes= WIN32OLE.new('iTunes.Application')
puts itunes.ole_methods


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

While ole works fine, because I can run:
require 'win32ole'

excel= WIN32OLE.new('excel.application')
puts excel.ole_methods
excel['Visible']=TRUE
wb = excel.Workbooks.Add()
sleep 10
excel.quit

Any ideas?
-Tim

Just Another Victim of the Ambient Morality

7/6/2006 9:25:00 PM

0


"Robert Klemme" <shortcutter@googlemail.com> wrote in message
news:4h3v7nF1q0k17U3@individual.net...
>
> 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...



Patrick Spence

7/14/2006 3:28:00 PM

0

Tim Hoolihan wrote:
> 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

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

dblack

7/14/2006 3:54:00 PM

0