Ruby Ruby Blocks Working With Blocks Block Method Practice: Arrays

MacKenzie T. Stout
MacKenzie T. Stout
23,972 Points

Block Method Practice: How do I add items to a new array?

I've used the select method but can't figure out how to add these items to a new array name "house"

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

1 Answer

Jennifer Nordell
STAFF
Jennifer Nordell
Treehouse Staff

Ok, a couple of things to note. The challenge asks you to set up a new variable named "house" then take the items greater in length than 4 and add them to the new array named house. We do this with the push method. Take a look at my code:

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

I set up the new variable named house. Then I use the select method on the given array to select items whose length is greater than 4 then push them into my new array. Important to note that the original array remains unchanged.

MacKenzie T. Stout
MacKenzie T. Stout
23,972 Points

That makes sense. I tried to create a new array with house.new and couldn't get the syntax correct. I knew that the push method was part of the solution but couldn't find the answer. Thank you!!