Ruby .select Method

Following was my Actualize daily exercise;

Use the the select method with the following array of hashes to create a new array of hashes that only contain movies with a rating less than 4.0.

movies = [
{id: 1, title: “Die Hard”, rating: 4.0},
{id: 2, title: “Bad Boys”, rating: 5.0},
{id: 3, title: “The Chamber”, rating: 3.0},
{id: 4, title: “Fracture”, rating: 2.0}
]

Select method does exactly what it says it does. It selects. We can use this method to do many things.

First step is to link it to a hash or a array where you want to select from. From our example above, we have a hash that we need to select ratings below 4.0.

movies.select

We can select certain element from the hash like so;

{|hash| hash[:rating] < 4.0}

What we are doing in the above example is naming all of our indivual array in our has, hash and then selecting the ratings element in each hash. We can write less than or greater than and then assign the number which will return us whether the rating is less than or greater than a number.

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.