Help With Code

Hello everyone! I am new to Ruby and purchased a book to start learning
the actual language. In the book, it had us write a code in a program
called “Atom”. The program is as follows:

puts “Welcome to Shapes”
print "How big do you want 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 inside”
height = shape_size.to_i
width = shape_size.to_i
1.upto(height) do |row|

Drawing code goes here

end
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

Whenever I open the file in the Command Prompt with Ruby, I get the
error:

shapes.rb:24:in ‘’: undefined local variable or method ‘row’ for
main:Object (NameError)

I have been trying to solve this but I have come up with nothing. Can
anyone help?

If you indent your code, it is easier to see.

1.upto(height) do |row|

Drawing code goes here

end

‘end’ marks the end of the loop. This loop does nothing. The code is
outside the loop, but ‘row’ is only defined within the loop.

Move the code inside the loop and it works:

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
end

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs