, GOTO.
, :
'define_steps.rb':
@steps = []
def step(i, &block)
@steps[i] = block
end
def launch_steps!(min = 0, max = @steps.size - 1)
@steps[min..max].each.with_index(min) do |block, i|
if block
puts "Launching step #{i}"
block.call
end
end
end
step 1 do
puts 'Step 1 with lots of code'
end
step 2 do
puts 'Step 2 with lots of code'
end
step 3 do
puts 'Step 3 with lots of code'
end
step 4 do
puts 'Step 4 with lots of code'
end
Run them separately with launch_steps.rb:
require_relative 'define_steps'
launch_steps!
puts "-----------------"
launch_steps!(2,3)
It outputs:
Launching step 1
Step 1 with lots of code
Launching step 2
Step 2 with lots of code
Launching step 3
Step 3 with lots of code
Launching step 4
Step 4 with lots of code
Launching step 2
Step 2 with lots of code
Launching step 3
Step 3 with lots of code
launch_steps!without parameters, each specific step is launch_steps!(min,max)performed , performs each step between minand max, launch_steps!(min)performs a step, minand every step after.
source
share