gpt4 book ai didi

ruby-on-rails - Ruby 初学者 - 意外的输入结束,期待 keyword_end

转载 作者:数据小太阳 更新时间:2023-10-29 08:22:11 26 4
gpt4 key购买 nike

我是 ruby​​ 的 super 新手,我正在尝试按照有关如何构建矩形的说明进行操作。我尽可能仔细地跟进,我三重检查我输入正确。我什至重新输入了它。问题似乎到了最后。我的终端一直给我错误:shapes.rb:25: syntax error, unexpected end-of-input, expecting 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

最佳答案

你还需要一个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,因为它是一个 block (把它想象成一个代码单元)。在该 block 中,您正在为可枚举(例如数组)中的每个项目执行代码。在这种情况下,您的可枚举值是 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

关于ruby-on-rails - Ruby 初学者 - 意外的输入结束,期待 keyword_end,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37621770/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com