DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Slopsquatting: A New Supply Chain Threat From AI Coding Agents
  • AI Is Making PHP Cool Again
  • Multi-Agent Software Engineering: One Coding Agent Isn't Enough
  • Before the AI Coding Agent Writes Code: Structuring Scattered Requirements With PARA

Trending

  • Building an AI Incident Copilot: How I Automated the First 15 Minutes of Every Production Incident
  • Parquet vs Lance: How Storage Layout Changes the Read Path
  • 6 Types of AI Orchestration Every Tech Leader Needs to Know
  • How We Know What We Know

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

By 
Alexandre Gama user avatar
Alexandre Gama
·
Jun. 11, 20 · Tutorial
Likes (4)
Comment
Save
Tweet
Share
6.9K Views

Join the DZone community and get the full member experience.

Join For Free

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

Ruby
xxxxxxxxxx
1
 
1
array = [15, 7, 3, 2, 17, 12, 1]
2
count = 0;
3
for item in array
4
  if item > 5
5
    count += 1
6
  end
7
end
8
puts count  # prints out the count value, which is 4. Nice


#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.

Ruby
xxxxxxxxxx
1
13
 
1
def how_many_are_greater_than(number, array)
2
  count = 0;
3
  for item in array
4
    if item > number
5
      count += 1
6
    end
7
  end
8
  return count # in Ruby we don't need to use return btw
9
end
10
11
array = [15, 7 , 3, 2, 17, 12, 1]
12
count = how_many_are_greater_than(5, array) # a little bit better, isn't it?
13
puts count


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

Ruby
xxxxxxxxxx
1
 
1
array.count do |item|
2
  item > number
3
end


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:

Ruby
xxxxxxxxxx
1
 
1
def how_many_are_greater_than_rubyish(number, array)
2
  array.count do |item|
3
    item > number
4
  end
5
end
6
7
array = [15, 7 , 3, 2, 17, 12, 1]
8
count = how_many_are_greater_than_rubyish(5, array)
9
puts count


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:

Ruby
x
 
1
def how_many_are_greater_than_rubyish(number, array)  
2
  return array.count do |item|
3
    item > number
4
  end
5
end
6
7
array = [15, 7 , 3, 2, 17, 12, 1]
8
count = how_many_are_greater_than_rubyish(5)
9
puts count


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

Blocks Coding (social sciences)

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

Opinions expressed by DZone contributors are their own.

Related

  • Slopsquatting: A New Supply Chain Threat From AI Coding Agents
  • AI Is Making PHP Cool Again
  • Multi-Agent Software Engineering: One Coding Agent Isn't Enough
  • Before the AI Coding Agent Writes Code: Structuring Scattered Requirements With PARA

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook