Hackerrank Ruby Control Structures - Each Solution
1 min read

Hackerrank Ruby Control Structures - Each Solution

Hackerrank Ruby Control Structures - Each Solution

Ruby offers control structures that let you iterate through its collections. One such control structure is each.

As you already know, HackerRank conducts many contests, and for every user who participates in a contest we update their score once the contest ends. You will be given a method called scoring with an array passed as an argument. Elements of the array are of the class User.

User class has a method update_score.

Your task is to iterate through each of the elements in the array using each and call the method update_score on every element.

Hint

array.each do |user|
    # call update_score on `user` here
end

Solution in ruby

Approach 1.

def scoring(array)
array.each do |user|
    user.update_score
end
end

Approach 2

def scoring(array)
    # iterate through each of the element in array using *each* and call update_score on it
    array.each do |user|
        user.update_score
    end
end

Enjoying these posts? Subscribe for more


Adblocker detected! Please consider reading this notice.

We've detected that you are using AdBlock Plus or some other adblocking software which is preventing the page from fully loading.

That's okay. But without advertising-income, we can't keep making this site awesome.

We don't have any banner, Flash, animation, obnoxious sound, or popup ad. We do not implement these annoying types of ads!

We need money to operate the site, and almost all of it comes from our online advertising.

Please add thepoorcoder.com to your ad blocking whitelist or disable your adblocking software.

×