To print a hollow pyramid pattern in Ruby, you can use loops. The outer loop controls the rows, and the inner loops manage the spaces and stars to create the hollow effect.
In this example,
def print_hollow_pyramid(rows)
(1..rows).each do |i|
pattern = ' ' * ((rows - i) * 2)
(1..(2 * i - 1)).each do |j|
if j == 1 || j == (2 * i - 1) || i == rows
pattern += '* '
else
pattern += ' '
end
end
puts pattern
end
end
print_hollow_pyramid(5)
* * * * * * * * * * * * * * * *
In this tutorial, we learned How to print Hollow Pyramid Pattern in Ruby language with well detailed examples.