A
A
Alexander Grishin2016-12-27 15:57:06
Ruby on Rails
Alexander Grishin, 2016-12-27 15:57:06

I want my own return_my in Ruby. How to do it?

For example, how to do something like this:

module RootRouter
  def route
    obj = SlaveClass.new()
    #....
    return obj.one if something
    return obj.two if something_else
    return obj.thr if something_else_another
    #....
  end
end


class SlaveClass
  def initialize
    @obj = ActionClass.new()
  end

  def one method
    #do something
    #...
    @obj.some_method
    #do something
    #...
    #do something
    #...

    return 'no response'
  end
  def two method
  end
  def thr method
  end
end

class ActionClass
  def some_method
    #do something
    #...
    return_my 'some variable some_method' if example_false_value

    puts 'all ok we continue here'

    #do something
    #...
    
    return 'any text for return'
  end
  def second_method
    #do something
    #...
    return_my 'some variable second_method' if example_false_value

    puts 'all ok we continue here'

    #do something
    #...
    
    return 'any text for return'
  end
end

Those. so that return_my immediately throws it to the RootRouter on its return , no matter where in the code it is located ?, without if -s at each nesting level

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander Grishin, 2016-12-28
@beerdy

Artyom Petrenkov : Options for that and options to float up. And yes, begin ... rescue ... end blocks can be nested.
Thanks Artem!

V
vsuhachev, 2016-12-28
@vsuhachev

In ruby, it is for exiting an arbitrary number of nestings that there is a throw / catch, under the hood these are the same exceptions.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question