R
Robert Klemme
2007/11/14 said:Let's compare them again. I changed some variable names which will
hopefully remove that red herring from the conversation. I also made
the styles more consistent for better comparison. (I was going to
show both my style and your one-liner style, but it was too
distracting.)
Temporaries in the scope of the target all_files:
data_files = stems.map { |stem|
"#{ stem }.#{ guess_extension stem }"
}
basenames =
add_suffixes(source_files + add_prefixes(data_files))
all_files = basenames.map { |basename|
File.join dir, basename
}
Temporaries inside block chains:
all_files = stems.map { |stem|
"#{ stem }.#{ guess_extension stem }"
}.as { |data_files|
add_suffixes(source_files + add_prefixes(data_files))
}.map { |basename|
File.join dir, basename
}
(BTW 'suffix' here means the chars right before the dot; after the dot
I call the extension.)
Why do add_suffixes and add_prefixes have to be methods that work on a
collection instead of a single item? Do they modify the collection in
any way or do they need information from other entries to do their
work on a single entry?
If not, I would change that and then you could do this:
all_files = (
source_files +
stems.map {|stem| add_prefix "#{ stem }.#{ guess_extension stem }"}
).map {|name| File.join dir, add_suffix(name)}
This is still pretty complex (but the algorithm is anyway) and you do
not need any temporaries outside of block parameters. Also,
add_suffix and add_prefix will be relieved from iterating collections
and can be focussed on doing one thing properly.
Kind regards
robert