We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
  • HackerRank Home

    HackerRank

  • |
  • Prepare
  • Certify
  • Compete
  • Hiring developers?
  1. Prepare
  2. Ruby
  3. Methods
  4. Procs

Procs

Problem
Submissions
Leaderboard
Discussions

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.

For example:

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

Author

[deleted]

Difficulty

Easy

Max Score

30

Submitted By

8897

Need Help?


View discussions
View top submissions

rate this challenge

MORE DETAILS

Download problem statement
Download sample test cases
Suggest Edits
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy