On Sun, Sep 18, 2011 at 2:05 PM, Adam Prescott <adam / aprescott.com> wrote:
> On Sun, Sep 18, 2011 at 12:30 PM, Robert Klemme
> <shortcutter / googlemail.com> wrote:
>> irb(main):010:0> a = 10.times.to_a
>> => [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>> irb(main):011:0> a.map! {|i| i.odd? ? i : nil}.compact!
>> => [1, 3, 5, 7, 9]
>> irb(main):012:0> a
>> => [1, 3, 5, 7, 9]
>
> A bit simpler:
>
>>> a = (1..10).to_a # don't really need the to_a for this, but hey
> => [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>
>>> a.select! { |e| e.odd? }
> => [1, 3, 5, 7, 9]
>
>>> a
> => [1, 3, 5, 7, 9]

My bad.  I left the conversion out of my example.  With conversion you
cannot use #select.  This is a better example:

irb(main):004:0> a = 10.times.to_a
=> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
irb(main):005:0> a.map! {|i| i.odd? ? ('*' * i) : nil}.compact!
=> ["*", "***", "*****", "*******", "*********"]

But anyway, this scenario is also not what the OP apparently wants.

Kind regards

robert


-- 
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/