Make a movie class that has a title field as string and genre field as an string array/list then spawn a list of this class with the details about each movie filled out.
Else a simple hash array would work.
Code
class Movies
def initialize
@movies = []
end
def addMovie(title, genres)
movie_hash = {'title' => title, 'genres' => genres}
@movies.push movie_hash
end
def removeMovie(title)
@movies.each_with_index do |hash, index|
if hash['title'].eql? title
@movies.delete_at index
break;
end
end
end
def getMovieGenres(title)
@movies.each_with_index do |hash, index|
if hash['title'].eql? title
if hash['genres'].kind_of?(Array)
return hash['genres'].join(' ')
else
return hash['genres']
end
end
end
end
def toString()
result = ''
@movies.each do |hash|
result += hash['title'] + ' '
end
return result
end
end
movieList = Movies.new()
movieList.addMovie('movie1', ['horror', 'suspsense'])
movieList.addMovie('movie2', 'thriller')
movieList.addMovie('movie3', ['action', 'thriller', 'suspense'])
puts movieList.toString()
puts movieList.getMovieGenres('movie1')
puts movieList.getMovieGenres('movie2')
movieList.removeMovie('movie2')
puts movieList.toString()
Code
ruby movies.rb
Process started >>>
movie1 movie2 movie3
horror suspsense
thriller
movie1 movie3
<<< Process finished. (Exit code 0)
How this applies to your project is up to you to interpret.
This post was edited by AbDuCt on Mar 7 2014 08:31pm