# Re: Showing part of an array

**URL:** <https://www.ruby-forum.com/t/re-showing-part-of-an-array/99427>\
**Category:** Ruby\
**Created:** [July 22, 2006, 5:42pm UTC](https://www.ruby-forum.com/t/re-showing-part-of-an-array/99427 "2006-07-22T17:42:00Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [July 22, 2006, 5:42pm UTC](https://www.ruby-forum.com/t/re-showing-part-of-an-array/99427/1 "2006-07-22T17:42:00Z")

</div>

You can use this function:

class Array  
def show\_part  
if self.length\>2  
p self[0…2].join(’, ‘)+’, …’  
else  
p self.join(’, ')  
end  
end  
end

… anbd apply it like this :

colors = [“black”,“white”,“red”,“yellow”,“green”]  
colors.show\_part  
two\_colors = [“black”,“white”]  
two\_colors.show\_part  
three\_colors = [“black”,“white”,‘red’]  
three\_colors.show\_part

Best regards,

Axel

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [July 22, 2006, 6:05pm UTC](https://www.ruby-forum.com/t/re-showing-part-of-an-array/99427/2 "2006-07-22T18:05:31Z")

</div>

Thanks, Axel

How about if you’re using it on an array consisting of both an id and a  
name so that

colors[0…2].collect{ |color| color.name }.join(’, ')

Cheers,  
Rosina
