Search⌘ K
AI Features

Solution: Printing a Diamond Shape

Explore how to print a diamond shape in Ruby by understanding loop indices, conditional statements, and string concatenation. This lesson guides beginners through using print versus puts, managing spaces and hashes, and emphasizes the importance of meaningful variable naming and troubleshooting techniques.

Courtney's version

Ruby 3.1.2
space = " "
space_count = 4
7.times do |row|
if row < 4
space_count -= 1
hash_count = row * 2 + 1
print space * space_count
else
space_count += 1
hash_count = (7 - 1 - row) * 2 + 1
print space * space_count
end
puts '#' * hash_count
end

Explanation

Courtney’s version uses print instead of puts to output the spaces. The print and puts statements are similar except puts outputs a newline character (indicating that the line has ended). Try replacing print in Courtney’s solution by puts ...