Day 1 of 30: Ruby Coding Challenge - Arrays and Blocks

DZone 's Guide to

Day 1 of 30: Ruby Coding Challenge - Arrays and Blocks

This is the first part of this series of 30 Coding Challenges in Ruby for folks who are just getting started. Let's explore a simple challenge with Arrays

· Web Dev Zone ·
Free Resource

Hey friend!

This is the first part of this series of 30 Coding Challenges in Ruby for:

  • Folks who are just getting started.
  • Folks who just want to stretch their brain a little bit.
  • Folks who just want to have fun in preparing themselves for coding interviews.

What about the Ruby environment?

  • No excuses, you don’t need that.
  • Here is the link for a Ruby Online Compiler, which I’m going to share all of the code and you can also play around.
  • It’s all about problem-solving skills and concepts, not about programming languages :).

The most important goal: It’s all about having fun while solving simple but great coding challenges.

I’m the Problem, Nice to Meet You

Given an array of numbers, we want to count how many items are greater than 5

C'mon, give us something more difficult and interesting!

I know, I know what you’re thinking, but this is just to start our journey together.

I’m going to break this down into 4 steps:

  • Logic to solve the problem.
  • Solving with a classic for loops.
  • Organizing our code with methods.
  • Bonus: Meeting Blocks in Ruby.

Are you ready? Let’s get into it : )

#1 Logic to Solve the Problem

Yes, the logic is pretty simple. Again, just to stretch our legs and arms to the run

  • Given an array.
  • Go through all of the numbers.
  • For each number, you compare if the number is greater than 5.
  • You increment a variable (let’s call it count).
  • You print the variable count after going through the array.

#2 Classic For-Loop Expression

Again, don’t worry about the Ruby syntax. Focus on concepts and logic.

For the code below we’ve created a classic for-loop expression, and for each item in the given array, we’re checking if the number is greater than 5, just like the previous logic


#3 Organizing With a Method

Nothing special here. Just organizing the kitchen with a method.

Notice that we’re taking a number as an argument, which will be the number used to compare the items in the array.


Pretty simple, right?

What about doing that in a more Ruby(ish) way? 

#4 Hipster Time: Blocks in Ruby

Bonus hipster time.

Ruby (as many other languages) has its own way to do a few things and that is called Ruby Way or Rubyish Way to do things

An array in Ruby provides a method called count(), which receives a block that can be an expression to be evaluated


Notice that:

  • item: is just a single item from our array. This block   magically   automatically executes a for loop expression
  • item > number: is the expression to be evaluated for each item

The complete code:


But wait. Where is the count variable and how we return it? Brilliant question.

Ruby automatically increments an internal variable each time the expression item > number is evaluated to true and keeps the result in this internal variable.

When the execution is finished, Ruby automatically returns the result for us.

Don’t believe me? Check the code below:


Now, I’m explicitly using the return keyword, which will behave in the same way, returning the final result after the expression being evaluated for each item in the array.

This is a more declarative approach than an imperative one, but this is a discussion for an upcoming article.

Well done, Ruby! 

That’s it. Thanks for your visit, really appreciate your time and see you soon :)

Let’s keep in touch and don’t forget to signup for my weekly newsletter

Twitter Youtube Instagram Linkedin

algorithm, array, beginner, coding, language, programming, programming language, ruby, tutorial, tutorial for beginners

Published at DZone with permission of Alexandre Gama . See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}