To print a hollow inverted pyramid pattern in Ruby, we use nested loops to manage spaces and stars.
In this example,
def print_hollow_inverted_pyramid(num_rows)
(1..num_rows).reverse_each do |row|
pattern = ''
(0...(num_rows - row)).each { |_| pattern += ' ' }
if row == num_rows || row == 1
pattern += '*' * (2 * row - 1)
else
pattern += '*'
pattern += ' ' * (2 * row - 3)
pattern += '*'
end
puts pattern
end
end
print_hollow_inverted_pyramid(5)
********* * * * * * * *
In this tutorial, we learned How to print Hollow Inverted Pyramid Pattern in Ruby language with well detailed examples.