Reputation: 671
Here is the code:
class Movie
def initialize(title, rank=0)
@title = title
@rank = rank
end
def to_s
"#{@title} has a rank of #{@rank}"
end
def thumbs_up
@rank += 1
end
def thumbs_down
@rank -= 1
end
end
class Playlist
def initialize(name)
@name = name
@movies = []
end
def add_movie(movie)
@movies << movie
end
def play
puts "#{@name}'s playlist:"
@movies.each do |movie|
movie.thumbs_up
puts movie
end
end
end`
My question is regarding thumbs_up. This code is working, but I'm curious as to how thumbs up is accessible within the play method in the Playlist class. Is this because the movie being added to the array is an instance of Movie, and can thus have thumbs_up called on it? If so, its strange that that method can be called within a different class like that.
Thanks.
Upvotes: 0
Views: 43
Reputation: 36860
It's not that strange. thumbs_up
is a public method of objects in the Movie class, so wherever a movie object resides, you can call thumbs_up
on it.
The method is one of the characteristics of a Movie object, which consists of methods and states (states is the information stored in a class object, in the instance variables).
Upvotes: 1
Reputation: 9344
thumbs_up
is a public
method, and as such, can be invoked from the Movie
instance regardless of where the invokation occurs.
Upvotes: 1