1

Example(Rails):

def blabla

    @ads = ["1", "2"] if @ads.nil?
    @reklame = Reklamer.find(@koder.sample)
    @ads[0] = @reklame.id
    if @ads[0] == @ads[1]
    begin from start method from start
    end

end

Is there a method to restart the method/action so it begins from the top?

Rails beginner
  • 14,321
  • 35
  • 137
  • 257

3 Answers3

1

You could use ruby-goto but I would definitely recommend against it.

For more information on why not to use goto statements check out this question. It is C# based but I think it covers the point nicely.

Community
  • 1
  • 1
Robert Greiner
  • 29,049
  • 9
  • 65
  • 85
1

Not sure what you want to accomplish, but it sounds like you need a loop of some sort. Would something like this do what you need?

def blabla
   @ads ||= ["1", "1"]
   # careful with comparing a string ("2") to a numeric id
   while @ads[0] == @ads[1] do
     @reklame = Reklamer.find(@koder.sample)
     @ads[0] = @reklame.id 
   end
end

Basically you keep reseting @ads[0] until it is different from @ads[1].

Andrea Singh
  • 1,593
  • 13
  • 23
1

Try the keyword retry, this keyword will restart the iteration from the beginning so consider to rebuild your method to use iteration, for example like this:

def blabla
  @ads = ["1", "2"] if @ads.nil?
  @ads.each_slice do |prev_,next_|
    @reklame = Reklamer.find(@koder.sample)
    prev_ = @reklame.id
    retry if prev_ == next_
  end
end

Also the keyword redo will repeat current iteration.

megas
  • 21,401
  • 12
  • 79
  • 130