# Showing part of an array

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

<div class="post-metadata">

**Author:** ![bignall](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e68b1a/32.png) [@bignall](https://www.ruby-forum.com/u/bignall)\
**Post date:** [July 22, 2006, 5:27pm UTC](https://www.ruby-forum.com/t/showing-part-of-an-array/99426/1 "2006-07-22T17:27:49Z")

</div>

Let’s say you have an array which might contain:

colors = [“black”,“white”,“red”,“yellow”,“green”]

And you’d like to see the first three elements with a … attached to  
the end if there are more than 3.

“colors[0,2].join(’, ‘) + ’ …’ if colors.count \> 3”

Which of course is blank in there are less than 3 elements in the array.

How do I limit the if to only working on the ’ …’ part?

Sorry for the newbie question, but I have no idea what to even look for  
in the docs.

Thanks!

---

<div class="post-metadata">

**Author:** ![bignall](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e68b1a/32.png) [@bignall](https://www.ruby-forum.com/u/bignall)\
**Post date:** [July 22, 2006, 6:01pm UTC](https://www.ruby-forum.com/t/showing-part-of-an-array/99426/2 "2006-07-22T18:01:20Z")

</div>

Rosina Bignall wrote:

> How do I limit the if to only working on the ’ …’ part?

def print\_color(colors)  
if colors.size \> 3  
puts colors[0,3].join(’, ‘)+’ …’  
else  
puts colors.join(’, ')  
end  
end

* * *

Xicheng

---

<div class="post-metadata">

**Author:** ![bignall](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e68b1a/32.png) [@bignall](https://www.ruby-forum.com/u/bignall)\
**Post date:** [July 22, 2006, 6:17pm UTC](https://www.ruby-forum.com/t/showing-part-of-an-array/99426/3 "2006-07-22T18:17:16Z")

</div>

Rosina Bignall wrote:

> How do I limit the if to only working on the ’ …’ part?

puts “#{colors[0, 2].join(’, ‘)}#{’…’ if colors.length \> 3}”

---

<div class="post-metadata">

**Author:** ![bignall](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e68b1a/32.png) [@bignall](https://www.ruby-forum.com/u/bignall)\
**Post date:** [July 22, 2006, 6:27pm UTC](https://www.ruby-forum.com/t/showing-part-of-an-array/99426/4 "2006-07-22T18:27:04Z")

</div>

Daniel S. wrote:

> Rosina Bignall wrote:
> 
> > How do I limit the if to only working on the ’ …’ part?
> 
> puts “#{colors[0, 2].join(’, ‘)}#{’…’ if colors.length \> 3}”

That’s what I thought, but this is actually being passed as an argument  
to be evaluated:

@scaffold\_columns = [  
AjaxScaffold::ScaffoldColumn.new(self, { :name =\> “colors”,  
:eval =\> “#{breed.colors[0, 2].collect{ |color| color.name}.join(’,  
‘)}#{’…’ if breed.colors.length \> 3}”,  
:sortable =\> false } )  
]

so I am just left with an blank column.

Thanks for your help!  
Rosina

---

<div class="post-metadata">

**Author:** ![bignall](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e68b1a/32.png) [@bignall](https://www.ruby-forum.com/u/bignall)\
**Post date:** [July 22, 2006, 9:27pm UTC](https://www.ruby-forum.com/t/showing-part-of-an-array/99426/5 "2006-07-22T21:27:36Z")

</div>

I think this example is asking for the ternary op.

```
 colors = %w( black red white yellow green )
 colors[0,3].join(', ') + (colors.length > 3 ? ' ...' : '')
```

---

<div class="post-metadata">

**Author:** ![bignall](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e68b1a/32.png) [@bignall](https://www.ruby-forum.com/u/bignall)\
**Post date:** [July 23, 2006, 5:11am UTC](https://www.ruby-forum.com/t/showing-part-of-an-array/99426/6 "2006-07-23T05:11:53Z")

</div>

Dumaiu wrote:

> I think this example is asking for the ternary op.
> 
> ```
> colors = %w( black red white yellow green )
> colors[0,3].join(', ') + (colors.length > 3 ? ' ...' : '')
> 
> ```

Thank you! Thank you! Thank you! Yes, that’s exactly what it needs!

Cheers,  
Rosina
