[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Showing part of an array....

Rosina Bignall

7/22/2006 3:28:00 PM


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!

--
Posted via http://www.ruby-....

5 Answers

xicheng

7/22/2006 3:59:00 PM

0

Rosina Bignall wrote:
> 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?
>

def print_color(colors)
if colors.size > 3
puts colors[0,3].join(', ')+' ...'
else
puts colors.join(', ')
end
end

---
Xicheng

Daniel Schierbeck

7/22/2006 4:10:00 PM

0


Rosina Bignall wrote:
> 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?

puts "#{colors[0, 2].join(', ')}#{'...' if colors.length > 3}"

Rosina Bignall

7/22/2006 4:27:00 PM

0

Daniel Schierbeck 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

--
Posted via http://www.ruby-....

Dumaiu

7/22/2006 7:20:00 PM

0

I think this example is asking for the ternary op.

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

Rosina Bignall

7/23/2006 3:12:00 AM

0

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

--
Posted via http://www.ruby-....