-
Notifications
You must be signed in to change notification settings - Fork 68
/
Copy pathFilmLibrary2.rb
40 lines (31 loc) · 1.09 KB
/
FilmLibrary2.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
=begin
Here is your next task! Use the test to solve your task.
Create 1 instance methods:
search_movies_by_title(str) Search for movies based on title. Example 'ma'
returns Batman and Supeman
Try to reduce code duplication to a minimum.
Each method should be not longer than five lines of code. If you have more than
five, think about your approach. You can put some stuff beneath the protected
line (... methods to get all tags or movies or some presenter ).
The preloaded variable is @film_library. Here is a extract from the structure
@film_library = {
spaceballs: {
tags: ['hilarious', 'parody', 'cult', 'classic', 'science fiction']
},
starship_troopers: {
tags: ['satire', 'science fiction', 'cult' ]
},
=end
# My Solution
class FilmLibrary
def initialize films
@films = films
end
def search_movies_by_title(title="")
title = title.to_s.strip
result = []
@films.each {|key,value| result << key.to_s.split("_").each.map {|x| x.capitalize}.join(" ") if key.match /#{title}/}
result == [] ? "No result for #{title}" : result.sort.join(", ")
end
protected
end