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/.
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/\.
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/.
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
--
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!
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!
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.
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.
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: