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

'for' loops

Basically, I want to do the following # Add number to destination if number # is less than 4

This code works:

def array_copy(source)
  destination = []
  for i in source
    destination.push(i) if i < 4
    end    
  return destination
end

BUT this code does NOT

def array_copy(source)
  destination = []
  for i in source
    if i < 4 
      destination.push(i)
    end    
  return destination
end

Why do I need to specify the method first eg. destination.push(i) if i < 4

instead of using a conditional first eg. if i < 4 destination.push(i)

? They look like they should do the same thing to me. Maybe I'm missing a key part of this code "pattern".

1 Answer

Jason Seifer
STAFF
Jason Seifer
Treehouse Guest Teacher

Hi Lucas Ackerknecht in your second code snipped your are missing an "end" before the "return" line which is what is causing the problem.