[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Array.drop doesn't work

Li Chen

7/15/2008 3:46:00 PM

Hi all,

I try to use #drop to remove several elements from an array but it
doesn't work.
I check "ri Array.drop" and get no info about #drop. But I see this info
described in http://www.ruby-doc.org/core/classes/Array.ht...


ary.drop(n) => array

Drops first n elements from ary, and returns rest elements in an array.

a = [1, 2, 3, 4, 5, 0]
a.drop(3) # => [4, 5, 0]



I wonder what is going on.

Thank you very much,

Li
#########
C:\Documents and Settings\chen73>irb
irb(main):001:0> a=[1,2,3,4]
=> [1, 2, 3, 4]
irb(main):002:0> a.class
=> Array
irb(main):003:0> a.drop(1)
NoMethodError: undefined method `drop' for [1, 2, 3, 4]:Array
from (irb):3
irb(main):004:0> a.drop(2)
NoMethodError: undefined method `drop' for [1, 2, 3, 4]:Array
from (irb):4
irb(main):005:0>

#########
C:\Documents and Settings\chen73>ri Array.drop
Nothing known about Array.drop
--
Posted via http://www.ruby-....

13 Answers

Frederick Cheung

7/15/2008 3:50:00 PM

0


On 15 Jul 2008, at 16:46, Li Chen wrote:

> Hi all,
>
> I try to use #drop to remove several elements from an array but it
> doesn't work.
> I check "ri Array.drop" and get no info about #drop. But I see this
> info
> described in http://www.ruby-doc.org/core/classes/Array.ht...
>
>
> ary.drop(n) => array
>
> Drops first n elements from ary, and returns rest elements in an
> array.
>
> a = [1, 2, 3, 4, 5, 0]
> a.drop(3) # => [4, 5, 0]
>
>
>
Seems like this was added in 1.8.7 (if you look at the docs for 1.8.6
you'll see it's not there)

Fred


> I wonder what is going on.
>
> Thank you very much,
>
> Li
> #########
> C:\Documents and Settings\chen73>irb
> irb(main):001:0> a=[1,2,3,4]
> => [1, 2, 3, 4]
> irb(main):002:0> a.class
> => Array
> irb(main):003:0> a.drop(1)
> NoMethodError: undefined method `drop' for [1, 2, 3, 4]:Array
> from (irb):3
> irb(main):004:0> a.drop(2)
> NoMethodError: undefined method `drop' for [1, 2, 3, 4]:Array
> from (irb):4
> irb(main):005:0>
>
> #########
> C:\Documents and Settings\chen73>ri Array.drop
> Nothing known about Array.drop
> --
> Posted via http://www.ruby-....
>


Tachikoma

7/16/2008 2:28:00 AM

0

On Jul 15, 11:46 pm, Li Chen <chen_...@yahoo.com> wrote:
> Hi all,
>
> I try to use #drop to remove several elements from an array but it
> doesn't work.
> I check "ri Array.drop" and get no info about #drop. But I see this info
> described inhttp://www.ruby-doc.org/core/classes/Array.ht...
>
> ary.drop(n) => array
>
> Drops first n elements from ary, and returns rest elements in an array.
>
>    a = [1, 2, 3, 4, 5, 0]
>    a.drop(3)             # => [4, 5, 0]
>
> I wonder what is going on.
>
> Thank you very much,
>
> Li
> #########
> C:\Documents and Settings\chen73>irb
> irb(main):001:0> a=[1,2,3,4]
> => [1, 2, 3, 4]
> irb(main):002:0> a.class
> => Array
> irb(main):003:0> a.drop(1)
> NoMethodError: undefined method `drop' for [1, 2, 3, 4]:Array
>         from (irb):3
> irb(main):004:0> a.drop(2)
> NoMethodError: undefined method `drop' for [1, 2, 3, 4]:Array
>         from (irb):4
> irb(main):005:0>
>
> #########
> C:\Documents and Settings\chen73>ri Array.drop
> Nothing known about Array.drop
> --
> Posted viahttp://www.ruby-....


Here is the Changes List ,and Array#drop included
http://svn.ruby-lang.org/repos/ruby/tags/v...

Dave Bass

7/16/2008 1:00:00 PM

0

Frederick Cheung wrote:
> Seems like this was added in 1.8.7 (if you look at the docs for 1.8.6
> you'll see it's not there)

Workaround: in 1.8.6 use something like

arr = n.times { arr.shift }

to drop the first n entries from the array. Or Array.slice, which would
probably be more efficient if you have to drop a large number of
entries.

Dave
--
Posted via http://www.ruby-....

David A. Black

7/16/2008 1:28:00 PM

0

Hi --

On Wed, 16 Jul 2008, Dave Bass wrote:

> Frederick Cheung wrote:
>> Seems like this was added in 1.8.7 (if you look at the docs for 1.8.6
>> you'll see it's not there)
>
> Workaround: in 1.8.6 use something like
>
> arr = n.times { arr.shift }

#times returns its receiver, so that would set arr to n.


David

--
Rails training from David A. Black and Ruby Power and Light:
Intro to Ruby on Rails July 21-24 Edison, NJ
Advancing With Rails August 18-21 Edison, NJ
See http://www.r... for details and updates!

David A. Black

7/16/2008 1:44:00 PM

0

Hi --

On Wed, 16 Jul 2008, Frederick Cheung wrote:

>
> On 15 Jul 2008, at 16:46, Li Chen wrote:
>
>> Hi all,
>>
>> I try to use #drop to remove several elements from an array but it
>> doesn't work.
>> I check "ri Array.drop" and get no info about #drop. But I see this info
>> described in http://www.ruby-doc.org/core/classes/Array.ht...
>>
>>
>> ary.drop(n) => array
>>
>> Drops first n elements from ary, and returns rest elements in an array.
>>
>> a = [1, 2, 3, 4, 5, 0]
>> a.drop(3) # => [4, 5, 0]
>>
>>
>>
> Seems like this was added in 1.8.7 (if you look at the docs for 1.8.6 you'll
> see it's not there)

It's a backport from 1.9. I think that's mostly what 1.8.7 is. It's
really kind of 1.9.prev, rather than 1.8.6.succ :-) (though of course
it doesn't have YARV, etc.)


David

--
Rails training from David A. Black and Ruby Power and Light:
Intro to Ruby on Rails July 21-24 Edison, NJ
Advancing With Rails August 18-21 Edison, NJ
See http://www.r... for details and updates!

David Masover

7/19/2008 1:16:00 AM

0

On Wednesday 16 July 2008 08:00:13 Dave Bass wrote:
> Frederick Cheung wrote:
> > Seems like this was added in 1.8.7 (if you look at the docs for 1.8.6
> > you'll see it's not there)
>
> Workaround: in 1.8.6 use something like
>
> arr = n.times { arr.shift }

Untested implementation, then:

class Array
def drop(n)
self.slice!(0,n)
end
end

Todd Benson

7/19/2008 3:09:00 PM

0

On Wed, Jul 16, 2008 at 8:00 AM, Dave Bass <davebass@musician.org> wrote:
> Frederick Cheung wrote:
>> Seems like this was added in 1.8.7 (if you look at the docs for 1.8.6
>> you'll see it's not there)
>
> Workaround: in 1.8.6 use something like
>
> arr = n.times { arr.shift }
>
> to drop the first n entries from the array. Or Array.slice, which would
> probably be more efficient if you have to drop a large number of
> entries.
>
> Dave

Like David B. said. Also, if you left off the assignment, it would be
more like arr.drop! since it's destructive, which the docs don't
imply. I love shift and unshift for all kinds of things, but I would
go with indices (you could use slice, too).

I haven't played with 1.9 or 1.8.7, but I'm assuming that drop simply
returns arr[n..-1] without affecting the original array.

Todd

Todd Benson

7/19/2008 3:12:00 PM

0

On Fri, Jul 18, 2008 at 8:16 PM, David Masover <ninja@slaphack.com> wrote:
> On Wednesday 16 July 2008 08:00:13 Dave Bass wrote:
> Untested implementation, then:
>
> class Array
> def drop(n)
> self.slice!(0,n)
> end
> end

If the drop method alters the array, then you would have to modify
your workaround to do what the docs say.

class Array
def drop(n)
self.slice!(0, n)
self
end
end

Todd

Robert Dober

7/19/2008 3:28:00 PM

0

Is it just me, or is there general confusion?

First of all #drop is not receiver modifying as is e.g. shift.

507/7 > ruby1.9 -e 'x=[1,2,3];x.drop(1);p x'
[1, 2, 3]

And then drop (n) is not returning the first n elements of an array
but all but the first n

512/12 > ruby1.9 -e 'p [1,2,3].drop(1)'
[2, 3]

thus the workaround implementation of 1.8.7.pred would rather be:
class Array
def drop n; self[n..-1] end
end

HTH
Robert
--
http://ruby-smalltalk.blo...

---
AALST (n.) One who changes his name to be further to the front
D.Adams; The Meaning of LIFF

msnews.microsoft.com

7/19/2008 4:52:00 PM

0

from _The Ruby Programming Language_ :

In Ruby 1.9, the selection methods described previously are augmented
by first, take, drop, take_while, and drop_while. first returns the
first element of an Enumerable object, or, given an integer argument
n, an array containing the first n elements. take and drop expect an
integer argument.take behaves just like first; it returns an array of
the first n elements of the Enumerable receiver object. drop does the
opposite; it returns an array of all elements of the Enumerable except
for the first n:

p (1..5).first(2) # => [1,2]
p (1..5).take(3) # => [1,2,3]
p (1..5).drop(3) # => [4,5]

So it looks like it's acting exactly as defined. I think the behavior
your expecting is delete
arr = [1,2,3]
arr.delete(1) # => 1
arr # => [2, 3]


On Jul 19, 2008, at 11:28 AM, Robert Dober wrote:

> Is it just me, or is there general confusion?
>
> First of all #drop is not receiver modifying as is e.g. shift.
>
> 507/7 > ruby1.9 -e 'x=[1,2,3];x.drop(1);p x'
> [1, 2, 3]
>
> And then drop (n) is not returning the first n elements of an array
> but all but the first n
>
> 512/12 > ruby1.9 -e 'p [1,2,3].drop(1)'
> [2, 3]
>
> thus the workaround implementation of 1.8.7.pred would rather be:
> class Array
> def drop n; self[n..-1] end
> end
>
> HTH
> Robert
> --
> http://ruby-smalltalk.blo...
>
> ---
> AALST (n.) One who changes his name to be further to the front
> D.Adams; The Meaning of LIFF
>