问题描述
我从中解除了以下示例
def strip_accents params
thunk = lambda do | key,value |
case value
当String然后value.remove_accents!
当Hash then value.each(& thunk)
end
end
params.each(& thunk)
end
当我把它放在rails控制台(irb),并用哈希调用它,我得到以下:
ruby-1.9.2-p136:044> `ruby --version`
=> ruby 1.9.2p136(2010-12-25 revision 30365)[i686-linux] \\\
ruby-1.9.2-p136:045> strip_accents({:packs => {:qty => 1}})
ArgumentError:错误的参数数量(1为2)
from(irb):32:
from(irb):37:in`each'
from(irb):37:in`strip_accents'
from(irb):45
从/ longpathtrimedforclarity / rb:44:在`start'
从/longpathtrimedforclarity/console.rb:8:in`start'
从/longpathtrimedforclarity/commands.rb:23:in`< top(必需) '
从脚本/ rails:6:在`require'
从脚本/ rails:6:在`< main>'
我明白lambdas检查arity,但我在lambda定义中看到两个参数。如果我改变 lambda do
到 Proc.new do
,代码执行,我得到预期的结果。 p>
Josh的例子是2008年的,所以我假设这是Ruby 1.8和1.9的区别。
实际上,它似乎在1.8和1.9之间改变了,但是这个改变修复了1.9。 2,至少在我的测试:
def strip_accents params
thunk = lambda do | h |
key,value = h
case value
当String然后value.remove_accents!
当Hash then value.each(& thunk)
end
end
params.each(& thunk)
end
这种方法也是向后兼容Ruby 1.8.7的。
I lifted the following example from Josh Susser
def strip_accents params thunk = lambda do |key,value| case value when String then value.remove_accents! when Hash then value.each(&thunk) end end params.each(&thunk) end
when I put it in the the rails console (irb), and call it with a hash, I get the following:
ruby-1.9.2-p136 :044 > `ruby --version` => "ruby 1.9.2p136 (2010-12-25 revision 30365) [i686-linux]\n" ruby-1.9.2-p136 :045 > strip_accents({:packs=>{:qty=>1}}) ArgumentError: wrong number of arguments (1 for 2) from (irb):32:in `block in strip_accents' from (irb):37:in `each' from (irb):37:in `strip_accents' from (irb):45 from /longpathtrimedforclarity/console.rb:44:in `start' from /longpathtrimedforclarity/console.rb:8:in `start' from /longpathtrimedforclarity/commands.rb:23:in `<top (required)>' from script/rails:6:in `require' from script/rails:6:in `<main>'
I understand that lambdas check arity, but I see two arguments in the lambda definition. If I change
lambda do
toProc.new do
, The code executes, and I get the expected result.Josh's example is from 2008, so I'm assuming this is a difference in Ruby 1.8 and 1.9. What's going on here?
解决方案Indeed, it appears to have changed between 1.8 and 1.9, but this change fixes it for 1.9.2, at least in my tests:
def strip_accents params thunk = lambda do |h| key, value = h case value when String then value.remove_accents! when Hash then value.each(&thunk) end end params.each(&thunk) end
This approach turns out to be backward-compatible with Ruby 1.8.7, as well.
这篇关于Hash.each和lambdas之间的不一致性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!