在 Hash#fetch 中使用 lambda 作为默认值 ruby

Using a lambda as a default in Hash#fetch ruby

我正在自信地阅读 ruby,并且正在尝试如何定义可重用的过程。根据给出的示例,我这样写:

DEFAULT_BLOCK = -> { 'block executed' }

answers = {}

answers.fetch(:x, &DEFAULT_BLOCK)

我期待它 return block executed 因为 x 在哈希中找不到,而是 returned wrong number of arguments (given 1, expected 0) (ArgumentError)。可能是什么问题?我还没有给块一个参数。

Hash#fetch取一个块时,密钥被传递给块。但是你从 proc 创建的块不带任何块参数。将定义更改为:

DEFAULT_BLOCK = -> x { 'block executed' }

你有,只是你没看到:

WHAT_AM_I_PASSING = ->(var) { var.inspect }

answers = {}

answers.fetch(:x, &WHAT_AM_I_PASSING)
# => ":x"

Hash#fetch的块提供了一个参数,你没有找到的key。你可以在你的 lambda 中接受一个参数并忽略它,或者让它成为一个 proc:

DEFAULT_BLOCK = proc { 'block executed' }
answers.fetch(:x, &DEFAULT_BLOCK)
# => "block executed" 

proc 起作用的原因是 lambda 验证是否提供了正确数量的参数,而 proc 则没有。 fetch 方法使用一个参数(键)调用 proc/lambda。

    2.6.1 :014 > DEFAULT_BLOCK = -> { 'block executed' }
     => #<Proc:0x00005586f6ef9e58@(irb):14 (lambda)> 
    2.6.1 :015 > answers = {}
     => {} 
    2.6.1 :016 > ans = answers.fetch(:x) {DEFAULT_BLOCK}
     => #<Proc:0x00005586f6ef9e58@(irb):14 (lambda)> 
    2.6.1 :017 > ans.call
     => "block executed" 

    Actually we can pass default value for key so that if key not found in the hash it use this default value like,
    my_hash = {}
     => {} 
    2.6.1 :019 > my_hash[:key1] = 'val1'
     => "val1" 
    2.6.1 :020 > p my_hash
    {:key1=>"val1"}
     => {:key1=>"val1"} 
    2.6.1 :022 > my_hash.fetch(:key1)
     => "val1" 
    2.6.1 :023 > my_hash.fetch(:key2)
    KeyError (key not found: :key2)
    Did you mean?  :key1
    2.6.1 :024 > my_hash.fetch(:key2){'val2'}
     => "val2"