Search⌘ K

Solutions: Printing Shapes Exercises

Understand how to print shapes in Ruby by exploring solutions for a rhombus, hollow square, and heart. Learn to use loops, conditional logic, and spacing calculations to create these patterns.

Print a rhombus

Ruby 3.1.2
hashtag = "#"
space = " "
width.times do |row|
space_count = width - row - 1
print space * space_count
puts hashtag * width
end

Explanation

  • Line 4: We subtracted the current row’s value from the total number of rows to calculate spaces. Note that we also subtracted an additional 1 ...