DEV Community

Discussion on: First Half. Ruby noob question

Collapse
 
jeremyf profile image
Jeremy Friesen
def first_half(array)
  array[0..(1+array.length/2)]
end
Enter fullscreen mode Exit fullscreen mode

(This is from my phone so please check my memory.)

Collapse
 
djuber profile image
Daniel Uber

That's also just about what I would write for my own use, possibly putting the limit = 1 + array.length/2 on a separate line, then returning array[0..limit], to reduce the amount of things happening at a time.