I want to use the idiom &method(:method_name) if more than one object is required for method_name . Can I do this under Ruby 1.9?
For example, if I have
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"]
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, he tries to make move_file(["foo.txt", "foo_20110915.txt"]) instead of move_file("foo.txt", "foo_20110915.txt") .
How can I split it so that it has the correct arity?
Workarounds I know of:
- Replace
def move_file(old_filename, new_filename) with def move_file(*arguments) - Replace
each(&method(:move_file)) with each{|old_filename, new_filename| move_file(old_filename, new_filename)} each{|old_filename, new_filename| move_file(old_filename, new_filename)}
source share