Skip to content
Programmingoneonone
Programmingoneonone

Learn everything about programming

  • Home
  • CS Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
    • Cybersecurity
  • 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
Programmingoneonone

Learn everything about programming

HackerRank Procs problem solution in ruby

YASH PAL, 31 July 2024

In this HackerRank Procs problem solution in ruby programming 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.

Example:

CODE

def foo(a, b, my_proc)

    my_proc.call(a, b)

end

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

puts foo(15, 10, add)

OUTPUT

25

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 foo, my_proc.call(a, b) executes the proc.

Task

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

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

HackerRank Procs problem solution in ruby programming

Problem solution.

def square_of_sum (my_array, proc_square, proc_sum)
    sum = proc_sum.call(my_array)
    proc_square.call(sum)
end

proc_square_number = proc {|n| n**2}
proc_sum_array     = proc {|a| a.reduce(:+)}
my_array = gets.split().map(&:to_i)

puts square_of_sum(my_array, proc_square_number, proc_sum_array)

Second solution.

def square_of_sum (my_array, proc_square, proc_sum)
    sum = proc_sum.call(my_array)
    proc_square.call(sum)
end

proc_square_number = proc {|x| x**2}
    proc_sum_array     = proc {|array| array.inject(0, &:+) }
    
my_array = gets.split().map(&:to_i)

puts square_of_sum(my_array, proc_square_number, proc_sum_array)

coding problems solutions Ruby Solutions

Post navigation

Previous post
Next post

Are you a student and stuck with your career or worried about real-time things, and don't know how to manage your learning phase? Which profession to choose? and how to learn new things according to your goal, and land a dream job. Then this might help to you.

Hi My name is YASH PAL, founder of this Blog and a Senior Software engineer with 5+ years of Industry experience. I personally helped 40+ students to make a clear goal in their professional lives. Just book a one-on-one personal call with me for 30 minutes for 300 Rupees. Ask all your doubts and questions related to your career to set a clear roadmap for your professional life.

Book session - https://wa.me/qr/JQ2LAS7AASE2M1

Pages

  • About US
  • Contact US
  • Privacy Policy

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes