Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

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

Chase Marchione
Chase Marchione
155,055 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...