D7net
Home
Console
Upload
information
Create File
Create Folder
About
Tools
:
/
proc
/
self
/
root
/
opt
/
alt
/
ruby34
/
share
/
ri
/
system
/
Array
/
Filename :
new-c.ri
back
Copy
U:RDoc::AnyMethod[iI"new:ETI"Array::new;TT:publico:RDoc::Markup::Document:@parts[o:RDoc::Markup::Paragraph; [I"Returns a new array.;To:RDoc::Markup::BlankLine o; ; [I"DWith no block and no argument given, returns a new empty array:;T@o:RDoc::Markup::Verbatim; [I"Array.new # => [] ;T:@format0o; ; [I"XWith no block and array argument given, returns a new array with the same elements:;T@o;; [I"7Array.new([:foo, 'bar', 2]) # => [:foo, "bar", 2] ;T; 0o; ; [I"NWith no block and integer argument given, returns a new array containing ;TI"6that many instances of the given +default_value+:;T@o;; [I"Array.new(0) # => [] ;TI"*Array.new(3) # => [nil, nil, nil] ;TI"!Array.new(2, 3) # => [3, 3] ;T; 0o; ; [I"?With a block given, returns an array of the given +size+; ;TI"Icalls the block with each +index+ in the range <tt>(0...size)</tt>; ;TI"Rthe element at that +index+ in the returned array is the blocks return value:;T@o;; [I"^Array.new(3) {|index| "Element #{index}" } # => ["Element 0", "Element 1", "Element 2"] ;T; 0o; ; [I"UA common pitfall for new Rubyists is providing an expression as +default_value+:;T@o;; [ I"array = Array.new(2, {}) ;TI"array # => [{}, {}] ;TI"array[0][:a] = 1 ;TI"Karray # => [{a: 1}, {a: 1}], as array[0] and array[1] are same object ;T; 0o; ; [I"SIf you want the elements of the array to be distinct, you should pass a block:;T@o;; [ I"!array = Array.new(2) { {} } ;TI"array # => [{}, {}] ;TI"array[0][:a] = 1 ;TI"Marray # => [{a: 1}, {}], as array[0] and array[1] are different objects ;T; 0o; ; [I"CRaises TypeError if the first argument is not either an array ;TI"ior an {integer-convertible object}[rdoc-ref:implicit_conversion.rdoc@Integer-Convertible+Objects]). ;TI"FRaises ArgumentError if the first argument is a negative integer.;T@o; ; [I"`Related: see {Methods for Creating an Array}[rdoc-ref:Array@Methods+for+Creating+an+Array].;T: @fileI"array.c;T:0@omit_headings_from_table_of_contents_below0I"�Array.new -> new_empty_array Array.new(array) -> new_array Array.new(size, default_value = nil) -> new_array Array.new(size = 0) {|index| ... } -> new_array ;T0[ I"(p1 = v1, p2 = v2);T@FFI" Array;TcRDoc::NormalClass00