[Solved] ruby syntax error, unexpected $end, expecting keyword_end

The following code is showing me the error as follows:

"ruby -KU -- 'C:UsersIshmaelMy DocumentsAptana Studio 3 WorkspaceSimpleFirstFile'
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
Enquirer: saishme

||

Solution #1:

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.

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

Respondent: Dominik Honnef

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Leave a Reply

Your email address will not be published.