Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

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!!