I am in my last week of pre work before my live classes start next week and I am hung up on this one problem and I was hoping someone could tell me where I'm going wrong.
This is the what they want us to do: Create a new array of hashes called great_movies which only contains movies with a rating greater than 3 and where each hash contains only the title and boxart information. The program should end with: p great_movies.
This is the code we are given:
movies = [
{
"id" => 70111470,
"title" => "Die Hard",
"boxart" => "http://cdn-0.nflximg.com/images/2891/DieHard.jpg",
"uri" => "http://api.netflix.com/catalog/titles/movies/70111470",
"rating" => 5.0,
"bookmark" => []
},
{
"id" => 654356453,
"title" => "Bad Boys",
"boxart" => "http://cdn-0.nflximg.com/images/2891/BadBoys.jpg",
"uri" => "http://api.netflix.com/catalog/titles/movies/70111470",
"rating" => 2.0,
"bookmark" => [{ "id" => 432534, "time" => 65876586 }]
},
{
"id" => 65432445,
"title" => "The Chamber",
"boxart" => "http://cdn-0.nflximg.com/images/2891/TheChamber.jpg",
"uri" => "http://api.netflix.com/catalog/titles/movies/70111470",
"rating" => 4.0,
"bookmark" => []
},
{
"id" => 675465,
"title" => "Fracture",
"boxart" => "http://cdn-0.nflximg.com/images/2891/Fracture.jpg",
"uri" => "http://api.netflix.com/catalog/titles/movies/70111470",
"rating" => 3.0,
"bookmark" => [{ "id" => 432534, "time" => 65876586 }]
}
]
This is what I have come up with so far but I don't think I am going about it the right way? class Movies
def initialize(id, title, boxart, url, rating, bookmark)
@id = id
@title = title
@boxart = boxart
@url = url
@rating = rating
@bookmark = bookmark
end
def id
@id
end
def title
@title
end
def boxart
@boxart
end
def url
@url
end
def rating
@rating
end
def bookmark
@bookmark
end
end
Then I put in all of the information the way they showed us how to but for some reason it wouldn't let me post that here I just did four different instances of Movies.new and put all of the information into those.
Any help pointing me in the right direction would be appreciated and just as a side note we just recently learned about the whole class method thing that's why I tried to do it that way because at least in my mind with that being the most recent thing they showed us that it what I was trying to use.
CodePudding user response:
I think creating a whole new class is rather overkill. You simply need to understand the methods already available for an array.
I am not going to provide you a direct solution as this is homework and SO isn't here to do your homework for you. But let's consider an array:
foo = [1, 4, 8, 2, 3, 5]
I can get values larger than 3
by using the select
method and giving it an appropriate block.
greater_than_3 = foo.select { |x| x > 3 }
Now greater_than_3
is [4, 8, 5]
.
What if I want an array containing each of those values, multiplied by two? I can can use the map
method and another block.
greater_than_3 = foo.select { |x| x > 3 }
times_two = greater_than_3.map { |x| x * 2 }
Now times_two
is [8, 16, 10]
.
As this is homework, I'll leave you to see how you can apply these methods to solve your problem, rather than directly giving you an answer you can copy and paste.
Documentation for the Ruby Enumerable mixin which provides the Array class with these methods should help you on your way.