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 Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
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
Partner Zones AWS Cloud
by AWS Developer Relations
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
Partner Zones
AWS Cloud
by AWS Developer Relations
Building Scalable Real-Time Apps with AstraDB and Vaadin
Register Now

Trending

  • Building the World's Most Resilient To-Do List Application With Node.js, K8s, and Distributed SQL
  • What Is Envoy Proxy?
  • What to Pay Attention to as Automation Upends the Developer Experience
  • How Web3 Is Driving Social and Financial Empowerment

Trending

  • Building the World's Most Resilient To-Do List Application With Node.js, K8s, and Distributed SQL
  • What Is Envoy Proxy?
  • What to Pay Attention to as Automation Upends the Developer Experience
  • How Web3 Is Driving Social and Financial Empowerment

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

Alexandre Gama user avatar by
Alexandre Gama
·
Jun. 11, 20 · Tutorial
Like (4)
Save
Tweet
Share
5.87K 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.

Trending

  • Building the World's Most Resilient To-Do List Application With Node.js, K8s, and Distributed SQL
  • What Is Envoy Proxy?
  • What to Pay Attention to as Automation Upends the Developer Experience
  • How Web3 Is Driving Social and Financial Empowerment

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com

Let's be friends: