Skip to content
Programmingoneonone
Programmingoneonone
  • Home
  • CS Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programmingoneonone

HackerRank Ruby – Enumerable – Introduction problem solution

YASH PAL, 31 July 2024

In this HackerRank Ruby – Enumerable – Introduction problem solution we learned about various methods to iterate over a collection like unless, loop and the most commonly used each method.

Ruby, however, provides an Enumerable module which packages a bunch of methods which can be used with any other class by including it (referred to as mixing in). That means that programmers don’t have to write all those methods many different times for different objects. As long as the custom object defines an each method and includes Enumerable module, it can get access to all of its magic.

In this challenge, you have been provided with a custom object called colors that defines its own each method. You need to iterate over the items and return an Array containing the values.

HackerRank Ruby - Enumerable - Introduction problem solution

Problem solution.

def iterate_colors(colors)
  result = []
  # Your code here
  colors.each do |color|
    result.push color
  end
  result
end

Second solution.

def iterate_colors(colors)
return colors.each_entry{ |color| color}.to_a
end

Third solution.

def iterate_colors(colors)
    color_set = Array.new
    colors.each do |color|
      color_set << color  
    end
    return color_set
end

coding problems solutions Ruby Solutions

Post navigation

Previous post
Next post

Pages

  • About US
  • Contact US
  • Privacy Policy

Programing Practice

  • C Programs
  • java Programs

HackerRank Solutions

  • C
  • C++
  • Java
  • Python
  • Algorithm

Other

  • Leetcode Solutions
  • Interview Preparation

Programming Tutorials

  • DSA
  • C

CS Subjects

  • Digital Communication
  • Human Values
  • Internet Of Things
  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2025 Programmingoneonone | WordPress Theme by SuperbThemes