如何将匿名对象 splattify 以便可以在其上使用 &method?

发布于 2024-12-05 05:05:54 字数 1022 浏览 2 评论 0原文

method_name 需要多个对象时,我想使用 &method(:method_name) 习惯用法。我可以在 Ruby 1.9 下执行此操作吗?

例如,如果我的

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"]

代码

old_filenames.zip(new_filenames).each(&method(:move_file))

可以在 Ruby 1.8 下运行,但不能在 Ruby 1.9 下运行。在 Ruby 1.9 下,它尝试执行 move_file(["foo.txt", "foo_20110915.txt"]) 而不是 move_file("foo.txt", "foo_20110915.txt")

我如何将其splattify以使其具有正确的数量?

我知道的解决方法:

  1. def move_file(old_filename, new_filename) 替换为 def move_file(*arguments)
  2. 替换 each(&method(:move_file))
    每个{|旧文件名,新文件名| move_file(旧文件名,新文件名)}

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:

  1. Replace def move_file(old_filename, new_filename) with def move_file(*arguments)
  2. Replace each(&method(:move_file)) with
    each{|old_filename, new_filename| move_file(old_filename, new_filename)}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

涙—继续流 2024-12-12 05:05:54

相反,

each{|old_filename, new_filename| move_file(old_filename, new_filename)}

你应该能够做到

each{|pair| move_file(*pair)}

但我不知道你如何实现无块变体(我也需要它几次)。我猜 &- 简写是为了使语法更简单,并且并不意味着会被太多阻塞(例如,无论是将数组作为数组传递,还是将其展开)。 :)

Instead

each{|old_filename, new_filename| move_file(old_filename, new_filename)}

you should be able to do

each{|pair| move_file(*pair)}

But I don't know how you'd pull off blockless variant (I needed it couple of times as well). I guess &-shorthand was made to make the syntax simpler, and is not meant to be clogged much (whether it will be passed an array as an array, or splatted, for example). :)

山色无中 2024-12-12 05:05:54

<块引用>

如何将其展开以使其具有正确的数量?

我认为没有办法在兼容两个 Ruby 版本的同时做到这一点。你可以做的就是将它包装成一个 lambda

move_from_to = Proc.new {|*both| move_files(*both) }

问题是 - 块和过程是 Ruby 1.9 中解决的问题,因此那里的行为可能有所不同。另请参阅此处的 prc.lambda? http:// www.ruby-doc.org/core/classes/Proc.html 了解它对数量的影响。

这个问题也与你想要做的事情有关(解决方案是手动 resplat 和 unsplat ): Hash.each 和 lambda 之间的数量不一致

How do I splattify it so it has the correct arity?

I don't think there is a way to do this while being compatible to both Ruby versions. What you could do is wrap it into a lambda

move_from_to = Proc.new {|*both| move_files(*both) }

The thing is - block and proc arity is something that got addressed in Ruby 1.9 so there might be a difference in behavior there. Also see prc.lambda? here http://www.ruby-doc.org/core/classes/Proc.html for info on what it does to the arity.

This question is also related to what you want to do (the solution there is to resplat and unsplat manually): Inconsistency of arity between Hash.each and lambdas

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文