I'm wanting to use the &method(:method_name)
idiom when there's more than one object required by method_name
. Can I do this under Ruby 1.9?
For example, if I've got
def move_file(old_filename, new_filename)
STDERR.puts "Moving #{old_filename.inspect} to #{new_filename.inspect}"
# Implementation for careful moving goes here
end
old_filenames = ["foo.txt", "bar.txt", "hoge.ja.txt"]
new_filenames = ["foo_20110915.txt", "bar_20110915.txt", "hoge_20110915.ja.txt"]
the code
old_filenames.zip(new_filenames).each(&method(:move_file))
works under Ruby 1.8, but not under Ruby 1.9. Under Ruby 1.9, it's trying to do move_file(["foo.txt", "foo_20110915.txt"])
instead of move_file("foo.txt", "foo_20110915.txt")
.
How do I splattify it so it has the correct arity?
Workarounds I'm aware of:
- Replace
def move_file(old_filename, new_filename)
withdef move_file(*arguments)
- Replace
each(&method(:move_file))
with
each{|old_filename, new_filename| move_file(old_filename, new_filename)}