N
Nick Woolley
Hi,
Can anyone suggest an idiomatic ruby equivalent to this perl snippet?
my %index = map { $_ => compute_something($_) } @files;
Where @files is an array of filenames, and compute_something is a
subroutine which does some probably expensive operation on the file.
The result is a hash indexing the file name to the computed result for
that file. This and it's grep equivalent is something I find quite
quite handy in perl.
The best I came up with so far seems reletively clunky to me:
index = Hash.new()
files.each {|f| index[f] = compute_something(f) }
or:
index = files.inject(Hash.new) {|i,f| i[f] = compute_something(f); i }
I was wondering if there's a more succinct and readable way?
TIA,
Nick
Can anyone suggest an idiomatic ruby equivalent to this perl snippet?
my %index = map { $_ => compute_something($_) } @files;
Where @files is an array of filenames, and compute_something is a
subroutine which does some probably expensive operation on the file.
The result is a hash indexing the file name to the computed result for
that file. This and it's grep equivalent is something I find quite
quite handy in perl.
The best I came up with so far seems reletively clunky to me:
index = Hash.new()
files.each {|f| index[f] = compute_something(f) }
or:
index = files.inject(Hash.new) {|i,f| i[f] = compute_something(f); i }
I was wondering if there's a more succinct and readable way?
TIA,
Nick