如何解释Ruby"end"调用方法

vmc*_*oud 4 ruby chef-infra

在学习chefspec时我发现了以下代码:

describe 'example::default' do
  let(:chef_run) do
    ChefSpec::SoloRunner.new do |node|
      node.set['cookbook']['attribute'] = 'hello'
    end.converge(described_recipe)
  end
end
Run Code Online (Sandbox Code Playgroud)

结束调用该方法收敛,我一些新的红宝石和chefspec,而我GOOGLE了它太多的时间和没有得到答案,有人可以帮忙解释语法?

小智 6

它与以下相同:

x = ChefSpec::SoloRunner.new do |node|
  node.set['cookbook']['attribute'] = 'hello'
end
x.converge(described_recipe)
Run Code Online (Sandbox Code Playgroud)


San*_*osh 5

converge在新ChefSpec::SoloRunner对象上调用该方法.

看一下使用块初始化对象的下面的示例.

Array.new(4) { 5 }.length
# => 4 
Array.new(4) do
  5
end.length
# => 4
Array.new(4) do
  5
end.class
# => Array 
Run Code Online (Sandbox Code Playgroud)