Skip to content
Programmingoneonone
Programmingoneonone
  • Engineering Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
    • 100+ C++ Programs
  • Solutions
    • HackerRank
      • Algorithms Solutions
      • C solutions
      • C++ solutions
      • Java solutions
      • Python solutions
    • Leetcode Solutions
    • HackerEarth Solutions
  • Work with US
Programmingoneonone
Programmingoneonone

HackerRank Blocks problem solution in Ruby

YASH PAL, 31 July 202429 January 2026

In this HackerRank Blocks problem solution in ruby programming, Higher-order functions are one of the key components of functional programming. A higher-order function is a tool that takes other functions as parameters or returns them as a result.

Blocks are nameless methods that can be passed to another method as a parameter.

Passing a block to a method is a great way of data abstraction.

Blocks can either be defined with a keyword do … end or curly braces { … }.

Example:

a). Passing a block to a method that takes no parameter

CODE

def call_block

    puts “Start of method.”

    yield

    puts “End of method.”

end 

call_block do 

    puts “I am inside call_block method.”

end

OUTPUT

Start of method.

I am inside call_block method.

End of method.

In this example, a block is passed to the call_block method.

To invoke this block inside the method, we used a keyword, yield.

Calling yield will execute the code within the block that is provided to the method.

b). Passing a block to a method that takes one or more parameters.

CODE

def calculate(a,b)

    yield(a, b)

end

puts calculate(15, 10) {|a, b| a – b}   

OUTPUT

5

In this example, we have defined a method calculate that takes two parameters a and b.

The yield statement invokes the block with parameters a and b, and executes it.

Task

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

The factorial method computes: n! { n x n – 1 x … 2 x 1 }.

HackerRank Blocks problem solution in ruby programming

HackerRank Blocks problem solution in ruby.

def factorial
    yield
end

n = gets.to_i
factorial do 
    puts "#{(1..n).inject(:*) || 1}"
end

Blocks problem solution in ruby.

def factorial(n)
    x = (1..n).inject(:*) || 1
    yield(x)
end

n = gets.to_i
factorial(n) do |x|
    puts x
end

coding problems solutions Hackerrank Problems Solutions Ruby Solutions HackerRankruby

Post navigation

Previous post
Next post

Leave a Reply

Your email address will not be published. Required fields are marked *

Programmingoneonone

We at Programmingoneonone, also known as Programming101 is a learning hub of programming and other related stuff. We provide free learning tutorials/articles related to programming and other technical stuff to people who are eager to learn about it.

Pages

  • About US
  • Contact US
  • Privacy Policy

Practice

  • Java
  • C++
  • C

Follow US

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