Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to explain that Ruby "end" invoke method

When learning chefspec I have found the below code:

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

The end invoke the method converge, I am some new to ruby and chefspec, and I have googled it much time and got no answer, can someone help to explain the syntax?

like image 497
vmcloud Avatar asked Dec 19 '22 06:12

vmcloud


2 Answers

It's the same as:

x = ChefSpec::SoloRunner.new do |node|
  node.set['cookbook']['attribute'] = 'hello'
end
x.converge(described_recipe)
like image 66
iced Avatar answered Jan 14 '23 19:01

iced


The method converge is called on the new ChefSpec::SoloRunner object.

Have a look at the below example, of initializing an object with a block.

Array.new(4) { 5 }.length
# => 4 
Array.new(4) do
  5
end.length
# => 4
Array.new(4) do
  5
end.class
# => Array 
like image 31
Santhosh Avatar answered Jan 14 '23 19:01

Santhosh