Keywords, like next & break are part of the Ruby programming language, so if you want a complete understanding of Ruby you need to know how they work.
What are they used for?
Well, in Ruby, we have all kinds of loops.
Like:
- while
- until
- each
When writing a loop, you may want to skip an iteration, or to end the loop early.
That’s where the next
& break
keywords come in!
Let’s find out how to use them.
Ruby Next Keyword (Skip Iteration)
The next
keyword allows you to skip one iteration.
Example:
Let’s say you’re counting strings.
And for some reason you don’t want to count strings with a size of 4.
You could do this:
strings = ["one", "two", "four", "five"] strings.inject(0) do |sum, str| next if str.size == 4 sum + str.size end # nil
This code is NOT going to work.
You get nil.
Why?
Because next
returns nil
by default, just like calling return
without a return value.
As you may know…
Using inject
takes the last value returned by the block & that becomes the first argument of the next iteration.
In other words, sum
becomes nil
.
The solution?
Change the next
line to this:
next sum if str.size == 4
This makes next
return the value of sum
.
If performance is not the highest priority (often it isn’t), you could pre-filter the array to avoid having to use next
.
Or even better:
strings = ["one", "two", "four", "five"] strings.inject(0) { |sum, str| str.size == 4 ? sum : sum + str.size } # 6
This is a ternary operator.
I think Ruby gives you enough tools to avoid having to use next
.
Use them!
Ruby Break Keyword (Exit Loop Early)
The break
keyword is like next
, but it ends the loop & returns a value, instead of skipping just one iteration.
It can be used for an early return from a loop.
Why not use the
return
keyword? Because it will exit from the current method, not just the loop.
Or to end an unconditional loop.
Like this:
count = 0 loop do break if count == 10 puts count count += 1 end
This prints a list of numbers, from 0 to 9.
A better alternative would be to use the times
method, the upto
method, or a range plus the each method.
Example:
0.upto(9).each(&method(:puts))
This &method(:puts)
is just a shortcut to call puts
for each number.
Avoiding Confusion: Next As A Ruby Method
Just because something exists as a keyword, it doesn’t mean it can’t be used as a method name.
A few classes in Ruby implement next
as a method.
Like String
& Integer
.
Example:
1.next # 2
The result isn’t surprising, right?
How about this:
"zz".next # "aaa"
That’s a bit more interesting!
How does this work?
Well, every class implements its version of next
, which contains the logic for the next value.
For Integers, this is just +1
.
For Strings, it increments the last character by one (next in alphabet / +1
for numbers), then if there’s a carryover (9 -> 0
, z -> a
), it will increment the next character on the left.
Summary
You’ve learned about the next
& break
keywords in Ruby, how they help you skip iterations, or stop a loop early.
Now it’s your turn to practice your Ruby skills 🙂
Thanks for reading!