1

In the method below I'm using abort to prematurely end the action if a given condition is met.

However, abort also throws the user out of IRB.

Is there something else besides abort the will end or break the method, but not kick the user out of irb.

Thanks.

def contrary
  if @quantity.label  == "particular"
    abort("There is no contrary for this type of propostion. Try subcontrary")
  end
  quality = @quality.opposite

  if @truthvalue
    truthvalue = !@truthvalue
  elsif !@truthvalue
    truthvalue = "unknown"
  end

  contrary = Proposition.new(@quantity, @subject, quality, @predicate, truthvalue)
  return contrary
end
Jeff
  • 3,943
  • 8
  • 45
  • 68
  • Haven't used ruby in awhile, but break or next should work? http://stackoverflow.com/questions/1402757/how-to-break-out-from-a-ruby-block – Ty Shaikh Aug 29 '15 at 14:45
  • but I do like sending a message with `abort("this doesn't work, trying something else")` – Jeff Aug 29 '15 at 14:48

2 Answers2

1

A return doesn't have to be the final statement in a method, nor does it have to return a value (it can return nil).

So having the following as the first line of your method should achieve what you're looking for:

return if @quantity.label == "particular"

If you want to output a message first:

if @quantity.label == "particular"
  puts "There is no contrary for this type of propostion. Try subcontrary"
  return
end
frostmatthew
  • 3,260
  • 4
  • 40
  • 50
1

You should use raise instead of abort. abort terminates your program, by design, whereas raise throws an exception that is caught by IRB's evaluator.

C. K. Young
  • 219,335
  • 46
  • 382
  • 435