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

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