Array.drop doesn't work

L

Li Chen

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.html#M000343


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
 
F

Frederick Cheung

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.html#M000343


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
 
T

Tachikoma

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.html#M000343

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


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

Dave Bass

Frederick said:
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
 
D

David A. Black

Hi --

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.html#M000343


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
 
T

Todd Benson

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
 
T

Todd Benson

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
 
R

Robert Dober

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
 
M

Mike Cargal

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]
 
R

Robert Dober

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
 
T

Todd Benson

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
 
D

David Masover

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top