[Solved] Ruby Beginner- unexpected end-of-input, expecting keyword_end

I am SUPER new to ruby, and I am trying to following instructions on how to build a rectangle. I am following along as carefully as possible, I triple checked I typed it correctly. I even retyped it. The problem seems to be in the end. My terminal keeps giving me the error: shapes.rb:25: syntax error, unexpected end-of-input, expecting keyword_end

Can anyone help me? I think it might be an issue with the 1.upto, but i’m not sure. Thank you so much!!

    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"
    width = shape_size
    height=shape_size
    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

Solution #1:

You need one more end:

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"
width = shape_size
height=shape_size
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 # <--- here

Since you are learning here, I felt compelled to add more detail:

When you have a block, such as the 1.upto(height) do |row| in your code, that will always require an end, as it is a block (think of it like a unit of code). Within that block, you are executing the code for each item within the enumerable (ex. array). In this case, your enumerable is an array of each whole number between 1 and the value of height:

2.3.0 :005 > 1.upto(4) do |number|
2.3.0 :006 >     puts "The number is: #{number}"
2.3.0 :007?>   end
The number is: 1
The number is: 2
The number is: 3
The number is: 4
Respondent: Brian

Solution #2:

Step 1: Fix Indenting
To fix this, first fix your indenting. This will usually make it obvious where the end is missing. You can see where your indenting is off by using the warning flag when running your ruby program: e.g. ruby -w my_program.rb.

Step 2: Decipher the warning
Look at the line numbers the warnings point to. From these, you may be able to figure out where your end is missing. If not, go to the next step.

Step 3: Isolate Code Causing Error
If you’re not able to figure out where the issue is due to the warnings, try commenting out chunks of code and seeing if you can get your program to run. Then uncomment and run your code chunk by chunk until the error returns. Using this method you’ll be able to isolate where your end is missing.

Respondent: wrongwrongworng

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Leave a Reply

Your email address will not be published.