Ruby Array
Topics
- Array Literal
- How to create new array
- 2-dimensional array
- Add element to array
- Refer value
- Delete element
- Each
Array Literal
Array class
a = [] b = [10, true, "30"] # Add any object b[0] #=> 10
How to create new array
[], Array.new
a = [1,2,3] a.class #=> Array array[1,2,3] Array.new(3, "str") # Init Array.new([1,2,3]) Array.new(3){ |i| * 3}
%
v1 = %w(homuhomu madoka mami) # v1 = ["homuhomu", "madoka", "mami"] v1.join() # homuhomumadokamami v1.join("_") # homuhomu_madoka_mami
Instantiate
a = Array.new(5) # [nil, nil, nil, nil, nil] a.length Array.new(2){|index| index + 10 } # [10, 11]
Initialize
a = Array.new(2, "a") #=> ["a", "a"] a[0], a[1]が同一のオブジェクト a[0].replace("b") #=> a[0]も, a[1]も変わる p a #=> ["b", "b"]
Block
a = Array.new(2){"a"} #=> ["a", "a"] a[0].replace("b") p a # ["b", "a"]
2-dimensional array
a = [[1,2], [3,4]] a[0][1] # 1 a[1][2] # 4
Add element to array
<<, push, concat
a = [1,2,3] a << 4 #=> [1,2,3,4] a.concat([5,6]) #=> [1,2,3,4,5,6] a.insert(3,9) #=> [1,2,3,9,4,5,6] a.unshift(10) #=> [10,1,2,3]
If there are no argument in unshift, do nothing.
Change element
a = [1,2,3] a[1] = 10 a[1..2] = [11,12]
Over index
v1 = [10] v1.length #=> 1 v1[3] = "aa" # Can add over index v1.length #=> 4 v1[2] #=> nil element between elements(inserted value) is nil v1[100] #=> nil
Refer value
[], slice, values_at, at, fetch, first, last, assoc, rassoc
a = [1,2,3] a[1] # 2 a.at(1) # 2 a[1..2] #=> [2,3] a.value_at(1) a.fetch(4) # IndexError a.fetch(4){|n| "ERROR" #{n}"} # "ERROR 4" a.first #=> 1 a.last #=> 3 a = [1,2,3,4,5] a.first(3) #=> [1,2,3] 3 elements from the beginning
assoc
Search array of array, if the argument match the first element of array, return this array.
This is example
a = [[1,2],[3,4], [5,6],[7,8]] a.assoc(3) #=> [3,4]
rassoc
This is similar with assoc, the difference is to match with index 1 element not first.
a = [[1,2], [3,4], [5,6], [7,8]] a.rassoc(4) #=> [3,4]
Look for element?
include?, index, rindex
a = [1,2,3,4,5] a.include?(3) #=> true a.include?(10) #=> false a.index(4) #=> 3 a.rindex(4) #=> 3 Search from the last
Return nil if there are no match value when using index,rindex.
Delete element
each, each_index, cycle, reverse_each, clear, slice!, shift, pop
List
Name | Description |
---|---|
delete_at | Delete element at index(break itself) |
delete_if | if evalutation is true, delete it (same as reject!) |
delete | delete if equal == method (not break) |
clear | Clear all elements |
slice! | Remove the element of index |
shift | Remove number of element from the beginning |
pop | Reverse of shift, remove from the last |
unshift | Add element from the first |
– | Remove element from myself |
delete_at
a = [1,2,3,4,5] a.delete_at(2) #=> [1,2,4,5]
delete_if
a = [1,2,3,4,5] a.delete_if{|n| n % 2 == 0} #=> [1,3,5]
delete
a = [1,2,3,4,5] a.delete(3) #=> [1,2,4,5]
slice!
a = [1,2,3,4,5] a.slice!(2,2) #=> [3,4] 2 elements from index 2
shift
a = [1,2,3,4,5] a.shift(2) #=> [1,2]
pop
a = [1,2,3,4,5] a.pop(2) #=> [4,5] a.pop #=> 3 a #=> [1,2]
–
a = [1,2,3,4,5] a - [1,2] #=> [3,4,5]
Each
each, each_index, cycle, reverse_each
for
a = [2,3,4] for i in a do p i end # 2 3 4
for i in a do
end
for i in [2,3,4] bar = 1 end p bar #=> 1 Oh! really?
for doesn’t create scope.
So, bar still alive.
each
[2,3,4].each do bar = 1 end p bar #=> NameError
cycle
[1,2,3].cycle{|n| puts n}
Connect(join)
[1,2,3].join(", ") #=> "1,2,3"