ruby语法错误,意外的$end,预期keyword_end

时间:2022-08-04 23:06:47

The following code is showing me the error as follows:

下面的代码显示的错误如下:

"ruby -KU -- 'C:\Users\Ishmael\My Documents\Aptana Studio 3 Workspace\Simple\FirstFile'
C:/Users/Ishmael/My Documents/Aptana Studio 3 Workspace/Simple/FirstFile:55: syntax error, unexpected $end, expecting keyword_end"

I searched for the extra end(s) and the trailing '.'s but in vain and I still can't figure out why it's not working!

我寻找额外的末端和尾部。但这是徒劳的,我还是弄不明白为什么它不工作!

class Greeter
  attr_accessor :name

  def initialize(name="dudes")
    @name=name
  end

  def say_hi

    if @name.nil?
      puts "..."
    else if @name.respond_to?("each")

      @name.each do |name|
        puts "Hello #{name.capitalize}!"
      end

    else
      puts "Hi #{@name.capitalize}!"
    end

  end 

  def say_bye

    if @name.nil?
      puts "..."
    else if @name.respond_to?("join")
      puts 'Goodbye #{@name.join(", ").capitalize}! Come back soon!'
    else
      puts "Bye #{@name.capitalize}!"
    end

  end

end

if __FILE__ == $0

  greeter = Greeter.new
  greeter.say_hi
  greeter.say_bye

  greeter.name = "Ishmael"
  greeter.say_hi
  greeter.say_bye

  greeter.name = ["Albert", "Brenda", "Charles", "Dave", "Englebert"]
  greeter.say_hi
  greeter.say_bye

  greeter.name = nil
  greeter.say_hi
  greeter.say_bye
end

1 个解决方案

#1


13  

It's not complaining about an extra end, but a premature end of the file, because of a missing end. In this case, You're forgetting to close the class before your if __FILE__ == $0 part.

它不是抱怨一个额外的结束,而是因为一个缺失的结束而过早的结束文件。在这种情况下,如果__FILE__ == $0部分,您将忘记关闭类。

This is probably because you confused elsif with else if, the latter one creating a whole new if block.

这可能是因为您混淆了elsif和else if,后者创建了一个全新的if块。

#1


13  

It's not complaining about an extra end, but a premature end of the file, because of a missing end. In this case, You're forgetting to close the class before your if __FILE__ == $0 part.

它不是抱怨一个额外的结束,而是因为一个缺失的结束而过早的结束文件。在这种情况下,如果__FILE__ == $0部分,您将忘记关闭类。

This is probably because you confused elsif with else if, the latter one creating a whole new if block.

这可能是因为您混淆了elsif和else if,后者创建了一个全新的if块。