Ruby With Missing End for The Loop Block?

This Ruby script asks the user for a size and two letters to create a shape. The outside letter forms the edges, while the inside letter fills the middle. It uses loops to construct the shape based on the specified dimensions and characters.

Ruby Code (with Errors):


codeputs "Welcome to Shapes"
print "How large would you like your shape? "

shape_size = gets
shape_size = shape_size.chomp

print "Outside letter: "
outside_letter = gets
outside_letter = outside_letter.chomp

print "Inside Letter: "
inside_letter = gets
inside_letter = inside_letter.chomp

puts "About to draw a shape #{shape_size} big"
puts "using #{outside_letter} for the edge"
puts "and #{inside_letter} for the fill"

height = shape_size.to_i
width = shape_size.to_i

1.upto(height) do |row|
if row == 1
puts outside_letter * width
elsif row == height
puts outside_letter * width
else
middle = inside_letter * (width - 2)
puts "#{outside_letter}#{middle}#{outside_letter}"
end

Rewritten Code (with corrections):

codeputs "Welcome to Shapes" 
print "How large would you like your shape? "

shape_size = gets.chomp

print "Outside letter: "
outside_letter = gets.chomp

print "Inside letter: "
inside_letter = gets.chomp

puts "About to draw a shape #{shape_size} big"
puts "using #{outside_letter} for the edge"
puts "and #{inside_letter} for the fill"

height = shape_size.to_i
width = shape_size.to_i

1.upto(height) do |row|
if row == 1 || row == height
puts outside_letter * width
else
middle = inside_letter * (width - 2)
puts "#{outside_letter}#{middle}#{outside_letter}"
end
end

Explanation of the Code:

In this simple Ruby program, the goal is to generate a rectangular shape based on the size provided by the user. The user can also select the characters to be used for the outer edges and the inner fill of the shape. Let’s break it down step by step:

  1. Input: The program starts by welcoming the user and asking for inputs:
    • The size of the shape (a single number will define both height and width).
    • The character to be used for the outer edge of the shape.
    • The character to be used for the inner fill of the shape.
  2. Shape Construction: After getting the user’s input, the program uses loops and conditionals to build the shape:
    • The 1.upto(height) loop runs through each row of the shape.
    • The first and last rows are fully composed of the outer edge character (outside_letter * width).
    • The rows in between consist of an outer edge at the start and end, with the inner fill (inside_letter) in between. The fill is calculated as inside_letter * (width - 2).
  3. Output: The completed shape is printed to the screen.

Fixing the Code Error:

The original code had an issue because the else block wasn’t properly closed with an end. Ruby needs explicit end statements to close blocks (like loops or conditionals). After adding the missing end, the program should now work as intended.

Example Output:

Let’s say the user enters the following:

codeHow large would you like your shape? 5
Outside letter: *
Inside letter: +

The output would look like:

code*****
*+++*
*+++*
*+++*
*****

In this case, the shape is a 5×5 square, with * forming the edges and + filling the middle.

This program can be easily customized to generate different shapes based on user input, offering a fun and interactive way to learn about loops, conditionals, and user input in Ruby.

Related blog posts