Ruby Ruby Blocks Working With Blocks Block Method Practice: Arrays

Henry Miller
Henry Miller
7,945 Points

This is so frustrating. I feel like the instructions are not clear enough...

Please help...

array_blocks.rb
array = ["Tree", "House"]
house = [item]
house.select {|array| array.length > 4}

2 Answers

CJ Marchione
CJ Marchione
144,353 Points

Hi Henry,

The word 'item' is often used to talk about pieces of data. In other words, the challenge is referring to the array values as items.

Something we can do is use the select method in the actual process of creating our house array, all on one line.

array = ["Tree", "House"]
house = array.select { |value| value.length > 4 }

We could replace 'value' with another name that we like--in this example, I'm using the word 'value' to refer to the data items we're testing before storing them into the house array, but I could have called the variable 'n', 'h', 'item', or whatever else I would've liked (within syntax rules, of course.)

Hope this helps!

Using the select method, create a new array named house that contains any items from the array variable with a length greater than four characters.

This basically means: Put every item of array which has more than 4 characters into a new variable called house using the select method.

array = ["Tree", "House"]

house = array.select {|item| item.length > 4}

Here select returns an array with every item in array which meets the given condition. You may have thought that an object (here house) "walks" to an array (here array) and selects the items it "wants".

How select actually works: It checks each item in array, one after another. Every item which passes get's returned in the end. So this example it looks like this:

      v
# ["Tree", "House"]    item = "Tree"; "Tree".length > 4     =>false

               v
# ["Tree", "House"]    item = "House"; "House".length > 4     =>true

# All items which returned true: "House"

Note that you can basically name variable in | | whatever you want, for example:

house = array.select {|word| word.length > 4} 
# or
house = array.select {|x| x.length > 4} 
# etc...