Ruby/Array/Array Creation

Материал из Wiki.crossplatform.ru

Перейти к: навигация, поиск

Содержание

A basic array with []

x = [1, 2, 3, 4]
# This array has four elements. 
# Each element is an integer, and is separated by commas from its neighboring elements. 
# All the elements are contained within square brackets.



Array.new creates an empty array, represented as [], named months.

# test whether an array is empty or not with the empty? method 
months = Array.new
months.empty? # => true



Arrays don"t need to be set up with predefined entries or have elements allocated manually.

x = []
# The array is empty
# Trying to address, say, x[5] results in nothing being returned. 
puts x[5]



assign an object (such as a string) to each element in the array

month = Array.new(12, "month")
# month now appears like this:
#["month", "month", "month", "month", "month", "month", "month", "month", "month",
# "month", "month", "month"]



Creates a array with the Array class"s new method: array2 = Array.new

array = ["Hello", "there", "AAA", 1, 2, 3]
puts array[0]
puts array[1]
puts array.length
array2 = Array.new
 
puts array2.length
array2[0] = "Banana"
array2[1] = "fish"
puts array2[0] + " " + array2[1]
puts array2.length



Creating an Array with a Block

# use a block with new, populating each element with what the block evaluates to
num = Array.new(10) { |e| e = e * 2 }



Creating Arrays with the new class method:

months = Array.new



Define an array of strings is with the %w notation.

# It assumes that all elements are strings (even nil)
 
months = %w[ nil January February March April May June July August September
October November December ]
# This produces the array months:
# ["nil", "January", "February", "March", "April", "May", "June", "July", "August",
#  "September", "October", "November", "December"]



Examples of the methods provided by Enumerable

puts [1,2,3,4].collect { |i| i.to_s + "x" }
puts [1,2,3,4].detect { |i| i.between?(2,3) }
puts [1,2,3,4].select { |i| i.between?(2,3) }
puts [4,1,3,2].sort
puts [1,2,3,4].max
puts [1,2,3,4].min



if you submit a set of strings, Array accepts them as a single, concatenated element.

d = Array( "H" "D" "L" )
p d



return the size of an array with either the size or length method

months = Array.new(12)
months.size # => 12
# or:
months.length # => 12



set the size of an array (the number of elements in an array)

months = Array.new(12)
# or like this:
months = Array.new 12



store text strings as well as numbers in arrays

array = ["Hello", "there", "AAA", 1, 2, 3]
puts array[1]       #prints "there"
puts array[4]       #prints 2



takes a range as an argument to create an array of digits.

digits = Array(0..9) # => [1, 2, 3, 4, 5, 6, 7, 8, 9]



To create an array, you use the [] operator

array = [1, 2, 3]
# This creates an array with three elements, 1, 2, and 3. 
# You access those elements using an array index like this:
puts array[0]      #prints 1
puts array[1]      #prints 2
puts array[2]      #prints 3