Ruby 初学者 - 意外的输入结束,期待 keyword_end [英] Ruby Beginner- unexpected end-of-input, expecting keyword_end

查看:38
本文介绍了Ruby 初学者 - 意外的输入结束,期待 keyword_end的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 ruby​​ 的超级新手,我正在尝试按照有关如何构建矩形的说明进行操作.我尽可能仔细地跟随,我三重检查我是否输入正确.我什至重新输入了它.问题似乎在最后.我的终端不断给我错误:shapes.rb:25: 语法错误,意外的输入结束,期待关键字结束

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

谁能帮帮我?我认为这可能是 1.upto 的问题,但我不确定.非常感谢!!

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

推荐答案

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

当您有一个块时,例如代码中的 1.upto(height) do |row|,它总是需要一个 end,因为它是一个块(把它想象成一个代码单元).在该块中,您正在为可枚举(例如数组)中的每个项目执行代码.在这种情况下,您的可枚举是一个包含 1 和 height 值之间的每个整数的数组:

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

这篇关于Ruby 初学者 - 意外的输入结束,期待 keyword_end的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆