Site icon NexGismo

How to Find Biggest Number in Ruby (Without Using .max)

Banner showing how to find the biggest number in Ruby without using the max method, with a magnifying glass icon highlighting number 23

When you’re writing code in Ruby, it’s easy to use built-in methods like .max to find the largest number in an array. But have you ever thought about how it actually works?

In this post, we’ll learn how to find biggest number in Ruby without using .max .

Don’t worry—it’s easier than you think, and a great way to sharpen your problem-solving skills.


Why Should You Learn This?

There are a few good reasons:

Even if you’re a beginner, this is a great exercise to boost your coding muscles.


🧠 The Problem

You have a list of numbers like this:

numbers = [5, 12, 3, 23, 9]

You want to find the biggest number — in this case, 23 — without doing this:

numbers.max  # ❌ Not allowed!

Let’s figure out how to do it step-by-step.


The Simple Plan

  1. Start by assuming the first number is the biggest.
  2. Go through the list one by one.
  3. If you find a bigger number, update your current “biggest” number.
  4. After checking all the numbers, you’ll have your answer!

Sounds simple, right?


Let’s Write the Code

Here’s a small method to do exactly that:

def find_biggest_number(array)
  return nil if array.empty?  # Handle empty list

  biggest = array[0]  # Start with the first number

  array.each do |num|
    if num > biggest
      biggest = num  # Found a new biggest number!
    end
  end

  biggest
end

How to Use It

numbers = [5, 12, 3, 23, 9]
puts "The biggest number is: #{find_biggest_number(numbers)}"

Output:

The biggest number is: 23

What If the List Is Empty?

If the list is empty, we return nil — because there’s no number to compare.

puts find_biggest_number([])  # => nil

That’s a safe way to handle edge cases.


Bonus: It Works with Negative Numbers Too!

puts find_biggest_number([-10, -2, -30, -1])

Output:

-1

Even when all the numbers are negative, our method still works perfectly.


🧑‍🏫 What You Learned


💬 Final Thoughts

Knowing how to build basic tools like this makes you a better developer. It helps you understand what’s going on behind the scenes—and that’s powerful.

So next time you reach for a built-in method, stop and ask:
“Could I build this myself?”

Even if you never use your custom method in a real app, the practice is what matters.


🙋‍♂️ Got a Question?

If you’re still unsure about something, or you wrote your own version and want feedback, feel free to share it with us!

And if you found this post helpful, consider sharing it with your friends or on LinkedIn — it might help someone else too.


🧭 Stay Curious!

Finding the biggest number in a Ruby array without using .max is a great way to understand how loops and comparisons work. It’s a skill that strengthens your foundation in Ruby programming. Keep practicing these small exercises — they often show up in real-life coding and technical interviews alike.

Happy coding & stay hungry for new things ! 💻✨

🔗 References

Exit mobile version