<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Real Louise Belcher</title>
    <description>The latest articles on DEV Community by Real Louise Belcher (@real_louise_belcher).</description>
    <link>https://dev.to/real_louise_belcher</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2805764%2F5ed960ba-0a85-4e23-982d-3fb8382211f3.png</url>
      <title>DEV Community: Real Louise Belcher</title>
      <link>https://dev.to/real_louise_belcher</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/real_louise_belcher"/>
    <language>en</language>
    <item>
      <title>Ruby Flow Control: next vs break vs return</title>
      <dc:creator>Real Louise Belcher</dc:creator>
      <pubDate>Mon, 24 Feb 2025 08:56:36 +0000</pubDate>
      <link>https://dev.to/real_louise_belcher/ruby-flow-control-next-vs-break-vs-return-414f</link>
      <guid>https://dev.to/real_louise_belcher/ruby-flow-control-next-vs-break-vs-return-414f</guid>
      <description>&lt;p&gt;Ruby gives you elegant ways to control your code flow. Three keywords stand out when working with blocks: &lt;code&gt;next&lt;/code&gt;, &lt;code&gt;break&lt;/code&gt;, and &lt;code&gt;return&lt;/code&gt;. Let's make them crystal clear.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Basics: Iterating Collections
&lt;/h3&gt;

&lt;p&gt;Most of the time in Ruby we work with and process collections of data. There are multiple methods that iterate over collection of data and process them. Here's a simple example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;numbers = (1..5).each do |number|
  puts "Processing number: #{number}"
end
# Processing number: 1
# Processing number: 2
# Processing number: 3
# Processing number: 4
# Processing number: 5
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But what if you need to skip items? Or stop processing early? That's where our these three very useful keywords come in: &lt;code&gt;next&lt;/code&gt;, &lt;code&gt;break&lt;/code&gt;, and &lt;code&gt;return&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  The &lt;a href="https://docs.ruby-lang.org/en/master/syntax/control_expressions_rdoc.html#label-next+Statement" rel="noopener noreferrer"&gt;next&lt;/a&gt; Keyword: Skip and Continue
&lt;/h3&gt;

&lt;p&gt;Think of &lt;code&gt;next&lt;/code&gt; as your "skip to the next item" command. It's perfect when you want to ignore certain elements but keep processing the rest.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;numbers = (1..5).each do |number|
  next if number.even?    # Skip even numbers
  puts "Found odd number: #{number}"
end
# Found odd number: 1
# Found odd number: 3
# Found odd number: 5
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, &lt;code&gt;next&lt;/code&gt; skips even numbers but continues processing the rest. It's like having a bouncer who lets some people skip the line.&lt;/p&gt;

&lt;h3&gt;
  
  
  The &lt;a href="https://docs.ruby-lang.org/en/master/syntax/control_expressions_rdoc.html#label-break+Statement" rel="noopener noreferrer"&gt;break&lt;/a&gt; Keyword: Stop Everything
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;break&lt;/code&gt; is your emergency exit. When you hit a &lt;code&gt;break&lt;/code&gt;, the block stops executing immediately. It's useful when you've found what you're looking for or hit a condition where continuing makes no sense.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;numbers = (1..5).each do |number|
  break if number &amp;gt; 3    # Stop when we exceed 3
  puts "Current number: #{number}"
end
# Current number: 1
# Current number: 2
# Current number: 3
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  The return Keyword: Exit the Method
&lt;/h3&gt;

&lt;p&gt;While &lt;code&gt;return&lt;/code&gt; works in blocks, it's primarily meant for methods. Using &lt;code&gt;return&lt;/code&gt; in a block will exit both the block AND the enclosing method. It's like pulling the emergency brake - everything stops.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def process_numbers
  (1..5).each do |number|
    return if number.even?    # Exit the entire method
    puts "Processing: #{number}"
  end
  puts "This line never runs if we hit return"
end

process_numbers
# Processing: 1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Choosing the Right Tool
&lt;/h3&gt;

&lt;p&gt;Here's when to use each:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Use &lt;code&gt;next&lt;/code&gt; when you want to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Skip the current iteration&lt;/li&gt;
&lt;li&gt;Continue with the next item&lt;/li&gt;
&lt;li&gt;Keep the loop running&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;Use &lt;code&gt;break&lt;/code&gt; when you want to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Stop the iteration completely&lt;/li&gt;
&lt;li&gt;Exit the current block&lt;/li&gt;
&lt;li&gt;Continue executing code after the block&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;Use &lt;code&gt;return&lt;/code&gt; when you want to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Exit the entire method&lt;/li&gt;
&lt;li&gt;Return a value from the method&lt;/li&gt;
&lt;li&gt;Note: Be careful with this in blocks!&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;h3&gt;
  
  
  Real-World Example
&lt;/h3&gt;

&lt;p&gt;Here's a practical example combining these concepts:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def find_user(users)
  result = users.each do |user|
    next if user.inactive?     # Skip to next user, continue searching
    break user if user.admin?  # Stop searching, return admin via result
    return user if user.moderator?  # Exit method immediately with moderator
    process_regular_user(user)
  end

  puts "Only reached if no moderator found"
  result # Returns admin (if found via break) or from processing regular_user
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Remember
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;next&lt;/code&gt; skips to the next iteration&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;break&lt;/code&gt; exits the current block&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;return&lt;/code&gt; exits the entire method&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Keep it simple. Use &lt;code&gt;next&lt;/code&gt; and &lt;code&gt;break&lt;/code&gt; for block control flow. Save &lt;code&gt;return&lt;/code&gt; for method returns. Your code will be clearer and your intentions more obvious.&lt;/p&gt;

&lt;p&gt;These tools are powerful. Use them wisely, and your Ruby code will flow like water.&lt;/p&gt;

</description>
      <category>programming</category>
      <category>ruby</category>
      <category>rails</category>
    </item>
  </channel>
</rss>
