语法错误,意外的$ end,期待来自-e:1的kEND(SyntaxError)

时间:2014-08-24 19:32:12

标签: ruby

class Movie

class Playlist
  def initialize(name)
    @name = name
    @movies = []
  end

  def add_movie(movie)
    @movies << movie
  end

  def play
    puts "#{@name}'s playlist:"
    puts @movies

    @movies.each do |movie|
      movie.thumbs_up
      puts movie
    end
  end
end

movie1 = Movie.new("Pride")
movie2 = Movie.new("Heaven")
movie3 = Movie.new("While")

playlist1 = Playlist.new("Brenda")
playlist1.add_movie(movie1)
playlist1.add_movie(movie2)
playlist1.add_movie(movie3)
playlist1.play

我是Ruby新手,不明白这段代码有什么问题。我收到了错误

syntax error, unexpected $end, expecting kEND (SyntaxError) from -e:1

1 个答案:

答案 0 :(得分:1)

您已启动class Movie而未关闭它。你的代码应该是这样的:

class Movie
end

class Playlist
  def initialize(name)
    @name = name
    @movies = []
  end

  def add_movie(movie)
    @movies << movie
  end

  def play
    puts "#{@name}'s playlist:"
    puts @movies

    @movies.each do |movie|
      movie.thumbs_up
      puts movie
    end
  end
end

movie1 = Movie.new("Pride")
movie2 = Movie.new("Heaven")
movie3 = Movie.new("While")

playlist1 = Playlist.new("Brenda")
playlist1.add_movie(movie1)
playlist1.add_movie(movie2)
playlist1.add_movie(movie3)
playlist1.play

但这也会产生一些问题。您正在使用许多未定义的Movie方法。您应该先完成Movie课程,然后才能在Playlist课程中使用它。