for (j = @array.length ; j > counter ; j = j-1) # counter is a variable
@array[j+1] = @array[j]
end
obviously this loop is invalid in ruby .. can any one tell me something
equivalent to it ?
···
--
Posted via http://www.ruby-forum.com/.
for (j = @array.length ; j > counter ; j = j-1) # counter is a variable
@array[j+1] = @array[j]
end
obviously this loop is invalid in ruby .. can any one tell me something
equivalent to it ?
--
Posted via http://www.ruby-forum.com/.
For this task, I would use #insert rather than iterating, as in
@array.insert(counter,@array[counter])
Dan Nachbar
On Sep 4, 2011, at 9:46 AM, jack jones wrote:
for (j = @array.length ; j > counter ; j = j-1) # counter is a variable
@array[j+1] = @array[j]
endobviously this loop is invalid in ruby .. can any one tell me something
equivalent to it ?
I didn't know the insert method what are its arguements.. are there
other way?
--
Posted via http://www.ruby-forum.com/.
thank you very much
--
Posted via http://www.ruby-forum.com/.
I've read about the method which inserts some element in a certain place
..
what I wanted to do is to shift all the elements to the back .. not
inserting which will require rather complicated operation like deleting
the element after shifting it .... I really don't understand the line
you wrote, thank you
--
Posted via http://www.ruby-forum.com/.
jack jones wrote in post #1020048:
for (j = @array.length ; j > counter ; j = j-1) # counter is a variable
@array[j+1] = @array[j]
endobviously this loop is invalid in ruby .. can any one tell me something
equivalent to it ?
There is no element in an array at the index array.length, and there is
no element in an array at the index array.length + 1. So what language
is that code valid in?
--
Posted via http://www.ruby-forum.com/\.
You might be able to answer this as well as your original question by
*reading the doc* for Array. Just sayin' ...
On Sun, Sep 4, 2011 at 7:19 AM, jack jones <shehio_22@hotmail.com> wrote:
I didn't know the insert method what are its arguements..
--
Hassan Schroeder ------------------------ hassan.schroeder@gmail.com
twitter: @hassan
I didn't know the insert method what are its arguements...
There are many excellent free online resources that do a much better
job of explaining it than I can. If you can't find the answer after
doing some searching on your own, please feel free to contact me offlist.
are there other way?
Yes, as with most tasks, there is many possible solutions.
Regards,
Dan Nachbar
On Sep 4, 2011, at 10:19 AM, jack jones wrote:
The #insert code, I think, did what your pseudo-code would have done.
But, given the above, I'm a bit confused as to what you are trying to do.
Perhaps an example would be clearer. For instance, given:
@array = [ 0,1,2,3,4,5 ]
counter = 3
What do you want @array to look like after your operation?
Dan Nachbar
On Sep 4, 2011, at 10:45 AM, jack jones wrote:
I've read about the method which inserts some element in a certain place
..
what I wanted to do is to shift all the elements to the back .. not
inserting which will require rather complicated operation like deleting
the element after shifting it .... I really don't understand the line
you wrote, thank you
jack jones wrote in post #1020055:
I've read about the method which inserts some element in a certain place
..
what I wanted to do is to shift all the elements to the back .. not
inserting which will require rather complicated operation like deleting
the element after shifting it .... I really don't understand the line
you wrote, thank you
It's pretty simple. It says, "Go to the position 2 in the array and
insert the value 'red'.
--
Posted via http://www.ruby-forum.com/\.
Insert nil. There are lots of options for docs.
rdoc.info: http://rdoc.info/stdlib/core/1.9.2/Array#insert-instance_method
ruby-doc.org: class Array - RDoc Documentation
And you can't have an array with a "gap" in it. ie what is returned if you
access that index after you've shifted the elements? The obvious answer is
nil, so just insert nil:
array = (0..10).to_a
counter = 5
array.insert counter, nil
array # => [0, 1, 2, 3, 4, nil, 5, 6, 7, 8, 9, 10]
On Sun, Sep 4, 2011 at 9:45 AM, jack jones <shehio_22@hotmail.com> wrote:
I've read about the method which inserts some element in a certain place
..
what I wanted to do is to shift all the elements to the back .. not
inserting which will require rather complicated operation like deleting
the element after shifting it .... I really don't understand the line
you wrote, thank you--
Posted via http://www.ruby-forum.com/\.
And if the length should stay the same an additional array.pop will
remove the last element.
Btw, for inserting multiple elements one can do:
irb(main):004:0> a=10.times.to_a
=> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
irb(main):005:0> a[5,0] = Array.new 3
=> [nil, nil, nil]
irb(main):006:0> a.pop 3
=> [7, 8, 9]
irb(main):007:0> a
=> [0, 1, 2, 3, 4, nil, nil, nil, 5, 6]
irb(main):008:0> a.length
=> 10
Kind regards
robert
On Sun, Sep 4, 2011 at 5:21 PM, Josh Cheek <josh.cheek@gmail.com> wrote:
On Sun, Sep 4, 2011 at 9:45 AM, jack jones <shehio_22@hotmail.com> wrote:
I've read about the method which inserts some element in a certain place
..
what I wanted to do is to shift all the elements to the back .. not
inserting which will require rather complicated operation like deleting
the element after shifting it .... I really don't understand the line
you wrote, thank you
Insert nil. There are lots of options for docs.
rdoc.info: http://rdoc.info/stdlib/core/1.9.2/Array#insert-instance_method
ruby-doc.org: http://ruby-doc.org/core/classes/Array.html#M000230And you can't have an array with a "gap" in it. ie what is returned if you
access that index after you've shifted the elements? The obvious answer is
nil, so just insert nil:array = (0..10).to_a
counter = 5
array.insert counter, nil
array # => [0, 1, 2, 3, 4, nil, 5, 6, 7, 8, 9, 10]
--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
Insert takes multiple arguments, also.
a = *1..10
a # => [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
index = 5
a.insert index, *a.pop(3)
a # => [1, 2, 3, 4, 5, 8, 9, 10, 6, 7]
On Mon, Sep 5, 2011 at 2:12 AM, Robert Klemme <shortcutter@googlemail.com>wrote:
On Sun, Sep 4, 2011 at 5:21 PM, Josh Cheek <josh.cheek@gmail.com> wrote:
> On Sun, Sep 4, 2011 at 9:45 AM, jack jones <shehio_22@hotmail.com> > wrote:
>
>> I've read about the method which inserts some element in a certain place
>> ..
>> what I wanted to do is to shift all the elements to the back .. not
>> inserting which will require rather complicated operation like deleting
>> the element after shifting it .... I really don't understand the line
>> you wrote, thank you> Insert nil. There are lots of options for docs.
> rdoc.info:
http://rdoc.info/stdlib/core/1.9.2/Array#insert-instance_method
> ruby-doc.org: class Array - RDoc Documentation
>
> And you can't have an array with a "gap" in it. ie what is returned if
you
> access that index after you've shifted the elements? The obvious answer
is
> nil, so just insert nil:
>
> array = (0..10).to_a
> counter = 5
> array.insert counter, nil
> array # => [0, 1, 2, 3, 4, nil, 5, 6, 7, 8, 9, 10]And if the length should stay the same an additional array.pop will
remove the last element.Btw, for inserting multiple elements one can do:
irb(main):004:0> a=10.times.to_a
=> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
irb(main):005:0> a[5,0] = Array.new 3
=> [nil, nil, nil]
irb(main):006:0> a.pop 3
=> [7, 8, 9]
irb(main):007:0> a
=> [0, 1, 2, 3, 4, nil, nil, nil, 5, 6]
irb(main):008:0> a.length
=> 10Kind regards
robert
--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/