Ruby代码问题

时间:2016-05-24 12:06:26

标签: ruby

下面给出了实现电影播放列表的代码,但这里的问题是当我试图运行这个程序时它给出如下输出。请帮助我获得正确的输出

class Movie
attr_accessor :movie_name, :rank

def initialize(movie_name,rank)
 @movie_name = movie_name
 @rank = rank
end

def thumbs_up
    @rank = @rank + 1
    display
end

def thumds_down
    @rank = @rank - 1
    display
end
def display
   puts "Name of Movie #{@movie_name} and Rank #{@rank}"
   end
end

class Playlist
    def initialize(name)
        @name = name
        @movies = []
    end
    def add_movies movie
        @movies << movie
    end
    def play
        puts "#{@name}'s Playlist:"
        puts @movies

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

    end
end


movie1 = Movie.new("Devil",10)
movie2 = Movie.new("Inception",3)

playlist1 = Playlist.new("John")
playlist1.add_movies(movie1)
playlist1.add_movies(movie2)
playlist1.play

OutPut:

John's Playlist:  
Name of Movie Devil and Rank 11  
Name of Movie Inception and Rank 4

预期OutPut:

John's Playlist:  
Name of Movie Devil and Rank 10  
Name of Movie Inception and Rank 3   
Name of Movie Devil and Rank 11  
Name of Movie Inception and Rank 4

4 个答案:

答案 0 :(得分:0)

Movie#display方法更改为:

def display
  "Name of Movie #{@movie_name} and Rank #{@rank}"
end

播放列表#播放:

def play
  movies = @movies.collect do |movie|
    movie.movie_name
    movie.thumbs_up
  end
  puts "#{@name}'s Playlist:#{movies.join(' and ')}"
end

答案 1 :(得分:0)

play方法中删除:

puts @movies

puts movie

答案 2 :(得分:0)

在你的每部电影中

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

将其替换为@movies.each { |movie| movie.display } 这将为您提供

的输出
playlist1.play
#Name of Movie Devil and Rank 10
#Name of Movie Inception and Rank 3

完整代码:

class Movie
 attr_accessor :movie_name, :rank

 def initialize(movie_name,rank)
   @movie_name = movie_name
   @rank = rank
 end

 def thumbs_up
   @rank = @rank + 1
   display
 end

 def thumds_down
   @rank = @rank - 1
   display
 end

 def display
   puts "Name of Movie #{@movie_name} and Rank #{@rank}"
 end
end

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

 def add_movies movie
  @movies << movie
 end

 def play
  @movies.each { |movie| movie.display }
 end
end

答案 3 :(得分:0)

我们在游戏方法中打印对象:

def play
    puts "#{@name}'s Playlist:"
    puts @movies
    @movies.each do |movie|
         movie.movie_name
         movie.thumbs_up
         puts movie
     end

end

上面使用puts @movies将打印对象地址,如果我们想要它打印一个字符串,那么我们必须在电影类中重载它。以下是正确的代码:

class Movie

attr_accessor :movie_name
attr_reader  :rank

def initialize(movie_name,rank=0)
    @movie_name = movie_name.upcase
    @rank = rank
end
def thumbs_up
    @rank = @rank + 1
end

def thumds_down
    @rank = @rank - 1
end
def to_s
"Name of Movie #{@movie_name} and Rank #{@rank}"
 end
end

class Playlist

 def initialize(name)
    @name = name
    @movies = []
 end
 def add_movies 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("Devil",10)  

   movie2 = Movie.new("Inception",3)

   playlist1 = Playlist.new("John")
   playlist1.add_movies(movie1)
   playlist1.add_movies(movie2)
   playlist1.play

   Output:
   John's Playlist:
   Name of Movie DEVIL and Rank 10
   Name of Movie INCEPTION and Rank 3
   Name of Movie DEVIL and Rank 11
   Name of Movie INCEPTION and Rank 4