seamus
seamus

Reputation: 2901

Ruby. (condition) ? return : next .Returning error

(1<2) ? return : next

dos.rb: dos.rb:74: Invalid next (SyntaxError)

What is the correct way to tell ruby to 'continue' in this context .

if 1 is less than 2, leave the function, else keep going

Upvotes: 0

Views: 268

Answers (3)

J&#246;rg W Mittag
J&#246;rg W Mittag

Reputation: 369428

return returns from a method or lambda, next returns from a block. There is neither a method nor a lambda nor a block in your code, therefore neither return nor next are valid in your code.

Upvotes: 4

Darshan Rivka Whittle
Darshan Rivka Whittle

Reputation: 34031

You can just say:

return if (1 < 2)

You don't have to tell Ruby to "keep going" -- that's what it'll do if it doesn't return from the function!

Upvotes: 5

Alan H.
Alan H.

Reputation: 16568

Just use if instead of a ternary, and be sure you are in a context (such as a loop) where next is valid.

Upvotes: 2

Related Questions