Array help

I have [1,2,3] and I want to return [1,2] is there already a method
that does this?

irb(main):001:0> [1,2,3]
=> [1, 2, 3]
irb(main):002:0> [1,2,3].pop
=> 3
irb(main):003:0> [1,2,3].everything_but_the_one_thing_that_pop_returns

~Jeremy

Hi –

On 18/10/2007, [email protected] [email protected]
wrote:

I have [1,2,3] and I want to return [1,2] is there already a method
that does this?

irb(main):001:0> [1,2,3]
=> [1, 2, 3]
irb(main):002:0> [1,2,3].pop
=> 3
irb(main):003:0> [1,2,3].everything_but_the_one_thing_that_pop_returns

[1,2,3][0…-2]
#=> [1, 2]

– Thomas A.

well I guess 2 solutions would be either

[1,2,2] - [1,2,3].pop.to_a

which just seems really ugly and not rubyish at all.

then I guess I could do [1,2,3].first(2) as long as my array stays
constant, but if it changes, then i’m screwed.

Any other ideas?

On Oct 18, 12:05 pm, “[email protected]

Hi –

On 18/10/2007, [email protected] [email protected]
wrote:

awesome. So what does the 0…-2 do? how does that work exactly?

When you use array subscripts starting from -1, -2, -3, etc, it starts
to look at the end of the array going backwards. So for instance:

a=[1,2,3]
=> [1, 2, 3]

a[-1]
=> 3

When you want a range inclusive of something, but not all the way to
end, you can tell it to stop off at a subscript which is relative to
the array’s size. Since you didn’t want the last element but
everything before it, that’s -2 as an array subscript counting
backwards:

a[0…-2]
=> [1, 2]

– Thomas A.

Hi,

Am Freitag, 19. Okt 2007, 04:05:55 +0900 schrieb
[email protected]:

I have [1,2,3] and I want to return [1,2] is there already a method
that does this?

irb(main):001:0> [1,2,3]
=> [1, 2, 3]
irb(main):002:0> [1,2,3].pop
=> 3
irb(main):003:0> [1,2,3].everything_but_the_one_thing_that_pop_returns

In case you really need the array and its reduced version
you could do somthing like

a = [1,2,3]
(d = a.dup).pop
d

The dup is inevitable and would be part of a inverse_pop
method if there were one.

Therefore I strongly recommend you consider another time
about what you want to achieve and how it is done best.
No harm meant,

Bertram

awesome. So what does the 0…-2 do? how does that work exactly?

Thanks for the help

~Jeremy

Jeremy W. wrote:

I have [1,2,3] and I want to return [1,2] is there already a method
that does this?

irb(main):001:0> [1,2,3]
=> [1, 2, 3]
irb(main):002:0> [1,2,3].pop
=> 3
irb(main):003:0> [1,2,3].everything_but_the_one_thing_that_pop_returns

I am not sure if you got your answer yet or not, so here goes.

What you are seeing is the result of the pop, i.e. what was popped.
What you want is the array AFTER the pop. This is how it goes:

a = [1, 2, 3]
p a.pop # 3 <= this is what was popped off
p a # [1, 2] <= This is what is left. use the array at this
point.

Hi,

On Fri, 2007-10-19 at 04:05 +0900, [email protected] wrote:

irb(main):003:0> [1,2,3].everything_but_the_one_thing_that_pop_returns

irb(main):001:0> [1,2,3][0…-1]
=> [1, 2]

HTH,
Arlen

[email protected] wrote:

I have [1,2,3] and I want to return [1,2] is there already a method
that does this?

irb(main):001:0> [1,2,3]
=> [1, 2, 3]
irb(main):002:0> [1,2,3].pop
=> 3
irb(main):003:0> [1,2,3].everything_but_the_one_thing_that_pop_returns

Hello Jeremy,

your problem is a mite unclear, I’ll try to cover a few possible
problems and their solutions

Returning parts

irb(main):001:0> [1,2,3][0…1] # return the first two elements
=> [1, 2]
irb(main):004:0> [1,2,3].slice(0,2) # same as above, slice and [] are
in the end the same method
=> [1, 2]
irb(main):005:0> [1,2,3][0…-2] # return everything but the last
=> [1, 2]

And now, the very ‘interesting’ way: map each value to it’s own

value, but to nil when it’s ‘3’ and remove the nil values from

the array

irb(main):011:0> [1,2,3].collect() {|i| i unless i == 3 }.compact
=> [1, 2]

Manipulating in-place

irb(main):015:0> a = [1,2,3]
=> [1, 2, 3]
irb(main):016:0> a.delete(3) # delete everything with the value ‘3’
=> 3
irb(main):017:0> a
=> [1, 2]
irb(main):021:0> a = [1,2,3]
=> [1, 2, 3]
irb(main):022:0> a.delete_if {|i| i == 3} # same as above
=> [1, 2]
irb(main):023:0> a
=> [1, 2]

Delete the element at position ‘2’, which incidentally is the last

one

irb(main):028:0> a = [1,2,3]
=> [1, 2, 3]
irb(main):036:0> a.delete_at(2) # could also use (a.size-1)
=> 3
irb(main):037:0> a
=> [1, 2]

Delete the element at the last position

irb(main):038:0> a = [1,2,3]
=> [1, 2, 3]
irb(main):039:0> a.delete_at(-1)
=> 3
irb(main):040:0> a
=> [1, 2]

… so far on a little fun with arrays. Note that in-place
manipulation actually works on the array itself, which might or might
not be what you want.

t.

Hi,

Am Freitag, 19. Okt 2007, 04:05:55 +0900 schrieb
[email protected]:

I have [1,2,3] and I want to return [1,2] is there already a method
that does this?

irb(main):001:0> [1,2,3]
=> [1, 2, 3]
irb(main):002:0> [1,2,3].pop
=> 3
irb(main):003:0> [1,2,3].everything_but_the_one_thing_that_pop_returns

Here’s another one, just for entertainment:

class Array ; def evth_but_pop ; slice 0, length-1 ; end ; end
[1,2,3].evth_but_pop

Bertram