Ruby Beginner-意外的输入结束,期待keyword_end

时间:2016-06-03 19:08:12

标签: ruby-on-rails ruby

我是红宝石的新手,我正在尝试按照如何构建矩形的说明。我正在尽可能仔细地跟进,我检查了三次,我输入正确。我甚至重新输入了它。问题似乎到底。我的终端一直给我错误: shapes.rb:25:语法错误,意外的输入结束,期待keyword_end

任何人都可以帮助我吗?我认为这可能是1.upto的一个问题,但我不确定。非常感谢你!!

    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

2 个答案:

答案 0 :(得分:6)

您还需要一个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

由于你在这里学习,我觉得有必要添加更多细节:

如果您有一个块,例如代码中的1.upto(height) do |row|,则总是需要end,因为它是一个块(将其视为一个代码单元)。在该块中,您正在为可枚举(例如数组)中的每个项执行代码。在这种情况下,您的枚举是一个介于1和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

答案 1 :(得分:1)

步骤1:修复缩进 要解决此问题,请先修复缩进。通常,这将使末端丢失的地方变得显而易见。您可以在运行ruby程序时使用警告标志查看缩进的位置:红宝石-w my_program.rb。

第2步:解读警告 查看警告所指向的行号。通过这些,您也许可以弄清楚您的目标丢失了。如果不是,请转到下一步。

第3步:隔离导致错误的代码 如果由于警告而无法确定问题出在哪里,请尝试注释掉大部分代码,看看是否可以使程序运行。然后取消注释并逐块运行代码,直到错误返回。使用这种方法,您将能够找出丢失末端的地方。