Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > (newbie) Array > Element Assignment > nil > Documentation

Reply
Thread Tools

(newbie) Array > Element Assignment > nil > Documentation

 
 
Potato Peelings
Guest
Posts: n/a
 
      06-09-2010
Ruby version : 1.9.1.376
OS : Windows

I'm checking http://ruby-doc.org/core/classes/Array.html#M002161 (Array
element assignment) which says
...If nil is used in the second and third form, *deletes* elements
from self...
with the example
a[1..-1] = nil #=> ["A"]

This doesn't seem to agree with how Ruby works now. puts a.to_s gives me
["A", nil]
nil seems to work the way any other assignment would. The splice is just
replaced with the new element.

Am I reading this wrong?
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
botp
Guest
Posts: n/a
 
      06-09-2010
On Wed, Jun 9, 2010 at 4:39 PM, Potato Peelings
<(E-Mail Removed)> wrote:
> I'm checking http://ruby-doc.org/core/classes/Array.html#M002161 (Array


that's an old and buggy doc

try this 1.9 doc

http://ruby-doc.org/core-1.9/classes/Array.html#M000418

btw, you can also use ri

eg


$ ri Array#[]=
Array#[]=

(from ruby core)
------------------------------------------------------------------------------
ary[index] = obj -> obj
ary[start, length] = obj or other_ary or nil -> obj or other_ary or nil
ary[range] = obj or other_ary or nil -> obj or other_ary or nil

------------------------------------------------------------------------------

Element Assignment---Sets the element at index, or replaces a subarray
starting at start and continuing for length elements, or
replaces a subarray specified by range. If indices are greater than
the current capacity of the array, the array grows automatically. A negative
indices will count backward from the end of the array. Inserts elements if
length is zero. An IndexError is raised if a negative index
points past the beginning of the array. See also Array#push, and
Array#unshift.

a = Array.new
a[4] = "4"; #=> [nil, nil, nil, nil, "4"]
a[0, 3] = [ 'a', 'b', 'c' ] #=> ["a", "b", "c", nil, "4"]
a[1..2] = [ 1, 2 ] #=> ["a", 1, 2, nil, "4"]
a[0, 2] = "?" #=> ["?", 2, nil, "4"]
a[0..2] = "A" #=> ["A", "4"]
a[-1] = "Z" #=> ["A", "Z"]
a[1..-1] = nil #=> ["A", nil]
a[1..-1] = [] #=> ["A"]

best regards
-botp

 
Reply With Quote
 
 
 
 
Potato Peelings
Guest
Posts: n/a
 
      06-10-2010
botp wrote:
> that's an old and buggy doc
>
> try this 1.9 doc
> http://ruby-doc.org/core-1.9/classes/Array.html#M000418


Thanks! That resolves it.


> btw, you can also use ri

This is nice. Thanks again.
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
nil.to_s != "nil" Eero Saynatkari Ruby 11 10-28-2006 10:57 PM
Integer(nil) versus Float(nil) versus String(nil) Christoffer Sawicki Ruby 5 09-02-2006 06:28 PM
a == nil or a.nil? ako... Ruby 6 11-23-2005 05:03 AM
RCR 303: nil should accept missing methods and return nil John Carter Ruby 64 05-19-2005 12:12 PM
puts nil generates "nil\n" Brian Candler Ruby 1 11-06-2004 01:59 PM



Advertisments