Looking for the Ruby way for the following


(Bil Kleb) #1

Currently I am using the following rather ugly bit of Ruby code
to calculate the maximum length of the file names strings (plus one)
in the array file_list,

fn_width = 0
file_list.each{ |fn| fn_width = [File.basename(fn).size, fn_width].max }
fn_width += 1

Can someone offer a more elegant method? My brain seems to have shutdown
on this problem today…

Thanks,

···


Bil


(David Alan Black) #2

Hello –

···

On Sat, 22 Jun 2002, Bil Kleb wrote:

Currently I am using the following rather ugly bit of Ruby code
to calculate the maximum length of the file names strings (plus one)
in the array file_list,

fn_width = 0
file_list.each{ |fn| fn_width = [File.basename(fn).size, fn_width].max }
fn_width += 1

Can someone offer a more elegant method? My brain seems to have shutdown
on this problem today…

You could do:

fn_width = file_list.map {|f| File.basename(f).size}.max + 1

David


David Alan Black
home: dblack@candle.superlink.net
work: blackdav@shu.edu
Web: http://pirate.shu.edu/~blackdav


(Austin Ziegler) #3

Why not use #inject()? It seems like it would be the natural choice
in this case.

-austin
– Austin Ziegler, austin@halostatue.ca on 2002.06.21 at 17.34.44

···

On Sat, 22 Jun 2002 06:17:25 +0900, Bil Kleb wrote:

Currently I am using the following rather ugly bit of Ruby code to
calculate the maximum length of the file names strings (plus one)
in the array file_list,

fn_width = 0
file_list.each{ |fn| fn_width = [File.basename(fn).size,
fn_width].max }
fn_width += 1

Can someone offer a more elegant method? My brain seems to have
shutdown on this problem today…


(Ned Konz) #4

Why not just:

1 + file_list.each { |fn| File.basename(fn).length }.max

···

On Friday 21 June 2002 02:17 pm, Bil Kleb wrote:

Currently I am using the following rather ugly bit of Ruby code
to calculate the maximum length of the file names strings (plus
one) in the array file_list,

fn_width = 0
file_list.each{ |fn| fn_width = [File.basename(fn).size,
fn_width].max } fn_width += 1

Can someone offer a more elegant method? My brain seems to have
shutdown on this problem today…


Ned Konz
http://bike-nomad.com
GPG key ID: BEEA7EFE


(David Alan Black) #5

Hello –

···

On Sat, 22 Jun 2002, Austin Ziegler wrote:

On Sat, 22 Jun 2002 06:17:25 +0900, Bil Kleb wrote:

Currently I am using the following rather ugly bit of Ruby code to
calculate the maximum length of the file names strings (plus one)
in the array file_list,

fn_width = 0
file_list.each{ |fn| fn_width = [File.basename(fn).size,
fn_width].max }
fn_width += 1

Can someone offer a more elegant method? My brain seems to have
shutdown on this problem today…

Why not use #inject()? It seems like it would be the natural choice
in this case.

I confess I am #inject-ly challenged. Can you show how this would
be done?

David


David Alan Black
home: dblack@candle.superlink.net
work: blackdav@shu.edu
Web: http://pirate.shu.edu/~blackdav


(Ned Konz) #6

er…

1 + file_list.collect { |fn| File.basename(fn).length }.max

sorry…

···

On Friday 21 June 2002 05:11 pm, Ned Konz wrote:

Why not just:

1 + file_list.each { |fn| File.basename(fn).length }.max


Ned Konz
http://bike-nomad.com
GPG key ID: BEEA7EFE


(Austin Ziegler) #7

Currently I am using the following rather ugly bit of Ruby code
to calculate the maximum length of the file names strings (plus
one) in the array file_list,

fn_width = 0
file_list.each{ |fn| fn_width = [File.basename(fn).size,
fn_width].max }
fn_width += 1

Can someone offer a more elegant method? My brain seems to have
shutdown on this problem today…
Why not use #inject()? It seems like it would be the natural
choice in this case.
I confess I am #inject-ly challenged. Can you show how this would
be done?

I believe that it would be something like (adapted from the Pickaxe
book):

file_list.inject(0) do

fn_width, fn| fn_width = [File.basename(fn).size, fn_width].max
end
fn_width += 1;

I suspect that the map solution you provided earlier is cleaner, and
you may need to do the appropriate #inject() mixin (p102 of the
Pickaxe book) if you don’t have 1.7 (I think that’s when #inject()
was added).

-austin
– Austin Ziegler, austin@halostatue.ca on 2002.06.21 at 19.08.37

···

On Sat, 22 Jun 2002 06:52:05 +0900, David Alan Black wrote:

On Sat, 22 Jun 2002, Austin Ziegler wrote:

On Sat, 22 Jun 2002 06:17:25 +0900, Bil Kleb wrote:


(David Alan Black) #8

Hello –

Why not use #inject()? It seems like it would be the natural
choice in this case.
I confess I am #inject-ly challenged. Can you show how this would
be done?

I believe that it would be something like (adapted from the Pickaxe
book):

file_list.inject(0) do

fn_width, fn| fn_width = [File.basename(fn).size, fn_width].max
end
fn_width += 1;

I suspect that the map solution you provided earlier is cleaner, and
you may need to do the appropriate #inject() mixin (p102 of the
Pickaxe book) if you don’t have 1.7 (I think that’s when #inject()
was added).

You could wed the cleanness of #map to the #inject version – you’d
mainly just want to rewrite it so that you were using the return value
of the call to #inject, rather than relying on the side-effect of
fn_width being incremented. (Note that with the above version you’d
also have to initialize fn_width beforehand.)

So, you could do something like:

biggest = file_list.inject(0) {|fn_width,fn|
fn_width = [File.basename(fn).size,fn_width].max
} + 1

David

···

On Sat, 22 Jun 2002, Austin Ziegler wrote:

On Sat, 22 Jun 2002 06:52:05 +0900, David Alan Black wrote:

On Sat, 22 Jun 2002, Austin Ziegler wrote:


David Alan Black
home: dblack@candle.superlink.net
work: blackdav@shu.edu
Web: http://pirate.shu.edu/~blackdav