Extract value from resulting array to a method

July 22, 2018

For example, we have method output like:

[false, 'Error'] or
[true, result]

We can use it this way:

def action
  passed, result = call_some_method

  if passed
    do_calculations
  else
    render json: { error: result, status: 400 }
  end
end

And inside do_calculations we can access result by calling method result:

def do_calculations
  clients: method_1,
  schedules: method_2
end

private

def method_1
  ::CallSomeService.new(client, result).call
end

def method_2
  ::CallAnotherService.new(client, result).call
end

memoize def result
  call_some_method.yield_self { |_, result| result }
end

memoize def call_some_method
  # ...

end
Extract value from resulting array to a method - July 22, 2018 - {"name"=>"Stanislav Knyazev", "email"=>"extazystas@gmail.com"}