Ruby Procs – HackerRank Solution

In this post, we will solve Ruby Procs HackerRank Solution. This problem (Ruby Procs) is a part of HackerRank Ruby series.


Passing blocks is one way to pass functions as arguments to other functions.

Blocks are one of the very few exceptions to the “everything is an object” rule in Ruby. Blocks are not objects, and they can’t be saved to variables.

Proc objects are blocks of code that can be bound to a set of local variables. You can think of a proc object as a “saved” block.

Procs are a great way of keeping your code DRY. DRY stands for Do not Repeat Yourself.



def foo(a, b, my_proc), b)

add = proc {|x, y| x + y}

puts foo(15, 10, add)



In this example, we have created the proc add, which adds two numbers.
The proc add is passed as a parameter to the method foo.
In the method, b) executes the proc.


You are given a partially complete code. Your task is to fill in the blanks (______).

=== codingbroz.com_728x90 (#88864) ===
=== codingbroz.com_728x90 (#88864) ===

The square_of_sum method computes the sum of the elements in an input array and returns the square of the summed elements.

For example:

Input array: [1, 2, 3]
Output: 36

Solution – Ruby Procs – HackerRank Solution

def square_of_sum (my_array, proc_square, proc_sum)
  sum =

proc_square_number = proc {|x| x**2}
proc_sum_array     = proc { |my_array| my_array.sum }
my_array = gets.split().map(&:to_i)

puts square_of_sum(my_array, proc_square_number, proc_sum_array)

Note: This problem (Ruby Procs) is generated by HackerRank but the solution is provided by CodingBroz. This tutorial is only for Educational and Learning purpose.

Leave a Comment

Your email address will not be published.