2

以下代码向我显示错误如下:

"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"

我搜索了多余的结尾和尾随的 '.' 但徒劳无功,我仍然无法弄清楚为什么它不起作用!

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
4

1 回答 1

14

它不是在抱怨额外的end,而是因为缺少end. 在这种情况下,您忘记在您的if __FILE__ == $0部分之前关闭课程。

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

于 2012-11-21T19:23:21.537 回答