Jeff
Jeff

Reputation: 4003

ruby aequivalent to abort that does not exit irb

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

Upvotes: 1

Views: 95

Answers (2)

C. K. Young
C. K. Young

Reputation: 222973

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.

Upvotes: 1

frostmatthew
frostmatthew

Reputation: 3298

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

Upvotes: 1

Related Questions