Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 957
Danyon Satterlee Avatar asked Jun 03 '16 19:06

Danyon Satterlee


2 Answers

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
like image 155
Brian Avatar answered Oct 13 '22 00:10

Brian


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.

like image 25
wrongwrongworng Avatar answered Oct 12 '22 23:10

wrongwrongworng