Array.drop doesn't work

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 class Array - RDoc Documentation

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

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

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

On Jul 15, 11:46 pm, Li Chen [email protected] wrote:

irb(main):001:0> a=[1,2,3,4]

#########
C:\Documents and Settings\chen73>ri Array.drop
Nothing known about Array.drop

Posted viahttp://www.ruby-forum.com/.

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

Frederick C. 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

Hi –

On Wed, 16 Jul 2008, Dave B. wrote:

Frederick C. 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

Hi –

On Wed, 16 Jul 2008, Frederick C. wrote:

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 :slight_smile: (though of course
it doesn’t have YARV, etc.)

David

On Wed, Jul 16, 2008 at 8:00 AM, Dave B. [email protected]
wrote:

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

On Wednesday 16 July 2008 08:00:13 Dave B. wrote:

Frederick C. 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

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.blogspot.com/


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

from The Ruby P.ming 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 Sat, Jul 19, 2008 at 6:52 PM, Mike C. [email protected] wrote:

Please refrain from top posting especially when the stuff you are
posting is not at all related to the quoted text

I think the behavior your
expecting is delete
arr = [1,2,3]
arr.delete(1) # => 1
arr # => [2, 3]
Be careful it seems that you expect delete to do different things

arr = %w{ Hello Brave World}
arr.delete “Brave”
arr

HTH
Robert

On Sat, Jul 19, 2008 at 10:28 AM, Robert D. [email protected]
wrote:

class Array
def drop n; self[n…-1] end
end

I figured I was right the first time. It’s nice to have some
confirmation. The documentation is a little ambiguous about that one,
but I think most of us know what they are saying. I’ll be diving into
1.9 pretty soon. I want to keep an eye on the eventual movement
towards 2.0.

Off-topic: I think I missed the announcement that the core docs on
ruby-doc are for 1.8.7. A little frustrated that there’s no title
telling me what docs I’m looking at. I had to run into the “drop”
question before I figured out my bookmark suddenly pointed to 1.8.7.
It’s not critical for me, because I spend all my time learning and
testing Ruby in IRB, but I imagine it might bother some other people.
Lesson learned. Bookmark the main page.

Todd

On Fri, Jul 18, 2008 at 8:16 PM, David M. [email protected]
wrote:

On Wednesday 16 July 2008 08:00:13 Dave B. 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

On Saturday 19 July 2008 10:11:44 Todd B. wrote:

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

Alright, I just tested it out in irb1.9. It seems to not alter the
array, and
to return all but the first n, as Robert says. So it would be:

class Array
def drop(n)
self.slice(n, self.length-n)
end
end

Or, another way:

class Array
def drop(n)
self.last(self.length-n)
end
end