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 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
--
Posted via http://www.ruby-forum.com/.

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]

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 15 Jul 2008, at 16:46, Li Chen wrote:

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-forum.com/\.

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

···

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.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
--
Posted viahttp://www.ruby-forum.com/.

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-forum.com/\.

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.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 :slight_smile: (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.rubypal.com for details and updates!

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.rubypal.com for details and updates!

Untested implementation, then:

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

···

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 }

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

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

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

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

···

On Saturday 19 July 2008 10:11:44 Todd Benson 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

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

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

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 Sat, Jul 19, 2008 at 10:28 AM, Robert Dober <robert.dober@gmail.com> wrote:

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

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

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 6:52 PM, Mike Cargal <mike@cargal.net> wrote: