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
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, JeremyWoertink@gmail.com <JeremyWoertink@gmail.com> 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 Adam
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, "JeremyWoert...@gmail.com" <JeremyWoert...@gmail.com> 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~Jeremy
Hi,
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
Am Freitag, 19. Okt 2007, 04:05:55 +0900 schrieb JeremyWoertink@gmail.com:
--
Bertram Scharpf
Stuttgart, Deutschland/Germany
http://www.bertram-scharpf.de
Jeremy Woertink 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.
--
Posted via http://www.ruby-forum.com/\.
JeremyWoertink@gmail.com 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.
--
Anton Bangratz - Key ID 363474D1 - http://tony.twincode.net/
fortune(6):
Q: What do they call the alphabet in Arkansas?
A: The impossible dream.
Hi,
On Fri, 2007-10-19 at 04:05 +0900, JeremyWoertink@gmail.com 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
Hi,
Am Freitag, 19. Okt 2007, 04:05:55 +0900 schrieb JeremyWoertink@gmail.com:
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
--
Bertram Scharpf
Stuttgart, Deutschland/Germany
http://www.bertram-scharpf.de
awesome. So what does the 0..-2 do? how does that work exactly?
Thanks for the help
~Jeremy
On Oct 18, 12:17 pm, "Thomas Adam" <thomas.ada...@gmail.com> wrote:
Hi --
On 18/10/2007, JeremyWoert...@gmail.com <JeremyWoert...@gmail.com> 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 Adam
Hi --
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 Adam
On 18/10/2007, JeremyWoertink@gmail.com <JeremyWoertink@gmail.com> wrote: