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
答案 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
课程中使用它。