Forum: Ruby Array resizing

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
2c7c807a1df0c76a8fc823c709b501a9?d=identicon&s=25 Victor Shepelev (Guest)
on 2006-03-28 14:40
(Received via mailing list)
Maybe dumb question, but...

I have not found anything like Array.resize, i.e.

a = [1,2]

a.resize(5)     #=> [1,2,nil,nil,nil]
a.resize(5, 3)  #=> [1,2,3,3,3]

I can use Array.fill, but

a.fill(3, a.size, a.size+2)

looks a bit ugly, not?

Moreover, if I want to resize to size lesser then array is, I need
slice.

Why not to have all-purpose resize?

Victor.
1889d44478574daa962c5ea19d3307bb?d=identicon&s=25 Pierre Barbier de Reuille (Guest)
on 2006-03-28 14:54
(Received via mailing list)
Victor Shepelev a écrit :
>
>
You may try :

a = [1,2]
a.fill(3, -1, 3)  #=> [1,2,3,3,3]

So you may code yourself your resize :

class Array
  def resize(new_size, value=nil)
    if new_size < length
         slice!(new_size, length)
    else
         fill(value, -1, new_size-length)
    end
  self
  end
end

Pierre
B97225f66bb5caac601b12735d430a0d?d=identicon&s=25 Marcin MielżyÅ?ski (Guest)
on 2006-03-28 14:59
(Received via mailing list)
Victor Shepelev wrote:
>
> a.fill(3, a.size, a.size+2)
>
> looks a bit ugly, not?
>

a = [1,2,3]
a += [nil] * 3

> Moreover, if I want to resize to size lesser then array is, I need slice.
>

a = [1,2,3,4,5]
a[0..-3]

Ruby 1.9 supports Array#pop taking an integer

lopex
B97225f66bb5caac601b12735d430a0d?d=identicon&s=25 Marcin MielżyÅ?ski (Guest)
on 2006-03-28 15:04
(Received via mailing list)
Pierre Barbier de Reuille wrote:

>
> a = [1,2]
> a.fill(3, -1, 3)  #=> [1,2,3,3,3]
>

nope:

a = [1,2]
a.fill(3, -1, 3)  #=> [1,3,3,3]


since -1 points to the last value in an array


lopex
2c7c807a1df0c76a8fc823c709b501a9?d=identicon&s=25 Victor Shepelev (Guest)
on 2006-03-28 15:16
(Received via mailing list)
Pierre wrote

>   end
> end

Yes, I can (in fact, I already did).

The question is more ideological than technical. Why not to have
Array#resize in standard library? It seems not very rational.

> Pierre

Victor.
1fba4539b6cafe2e60a2916fa184fc2f?d=identicon&s=25 unknown (Guest)
on 2006-03-28 15:23
(Received via mailing list)
Hi --

On Tue, 28 Mar 2006, Victor Shepelev wrote:

>>     end
>>   self
>>   end
>> end
>
> Yes, I can (in fact, I already did).
>
> The question is more ideological than technical. Why not to have
> Array#resize in standard library? It seems not very rational.

I think it might be because the size of an array isn't important if
the array just contains nils, because uninitialized values default to
nil anyway.  And if you're adding non-nil elements to the array, then
the fact that the size changes is not really the main point; it's just
a side-effect of the operation, so referring to it as a "resize"
operation doesn't fit very well.


David

--
David A. Black (dblack@wobblini.net)
Ruby Power and Light, LLC (http://www.rubypowerandlight.com)

"Ruby for Rails" chapters now available
from Manning Early Access Program! http://www.manning.com/books/black
2c7c807a1df0c76a8fc823c709b501a9?d=identicon&s=25 Victor Shepelev (Guest)
on 2006-03-28 15:56
(Received via mailing list)
David wrote:

> > The question is more ideological than technical. Why not to have
> > Array#resize in standard library? It seems not very rational.
>
> I think it might be because the size of an array isn't important if
> the array just contains nils, because uninitialized values default to
> nil anyway.  And if you're adding non-nil elements to the array, then
> the fact that the size changes is not really the main point; it's just
> a side-effect of the operation, so referring to it as a "resize"
> operation doesn't fit very well.

Sounds reasonable.
I can tell when I stumbled upon need for __resizing__ aray: when tried
to
use Array#transpose - it can't process
[
 [1,2,3],
 [1],
 [1,2,3,4]
]

I think, it isn't sole case, when we can want having array of arbitrary
size.

> David

Victor.
This topic is locked and can not be replied to.