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
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Related

  • Vibe Coding With GitHub Copilot: Optimizing API Performance in Fintech Microservices
  • Unlocking AI Coding Assistants Part 3: Generating Diagrams, Open API Specs, And Test Data
  • All About GPU Threads, Warps, and Wavefronts
  • Soft Skills Are as Important as Hard Skills for Developers

Trending

  • AI Agents: A New Era for Integration Professionals
  • Monoliths, REST, and Spring Boot Sidecars: A Real Modernization Playbook
  • Traditional Testing and RAGAS: A Hybrid Strategy for Evaluating AI Chatbots
  • Apple and Anthropic Partner on AI-Powered Vibe-Coding Tool – Public Release TBD

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

  • Vibe Coding With GitHub Copilot: Optimizing API Performance in Fintech Microservices
  • Unlocking AI Coding Assistants Part 3: Generating Diagrams, Open API Specs, And Test Data
  • All About GPU Threads, Warps, and Wavefronts
  • Soft Skills Are as Important as Hard Skills for Developers

Partner Resources

×

Comments
Oops! Something Went Wrong

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

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

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 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!