情報学部 | 菅沼ホーム | 目次 | 索引 |
x = Array[1, 2.3, "abc"]; # x = [1, 2.3, "abc"]; でも可
u1 = Array[1, "abc", 2]; u2 = Array[1, "abc", 2]; u3 = u1;
v1 = Array[[10, 20, 30], [40, 50, 60]]; v2 = Array[[10, 20, 30], [40, 50, 60]]; v3 = v1;
v4 = v1[0]; // v1,v3 の 1 行目
a = [1, 2, 3]; p a # => [1, 2, 3] b = Array[1, 2, 3]; p b # => [1, 2, 3]
a = Array.new(3, 10); p a # => [10, 10, 10] b = Array.new(["a", "b", "c"]); p b # => ["a", "b", "c"] k = -1; c = Array.new(3) { |m| k += 1 }; p c # => [0, 1, 2]
a = [1, 2, 3]; p a # => [1, 2, 3] p a[1] # => 2 b = [["a", "b"], ["c", "d"], ["e", "f"]]; p b # => [["a", "b"], ["c", "d"], ["e", "f"]] p b[1] # => ["c", "d"] p b[1][0] # => "c"
a = [1, 2, 3]; p a[0..1] # => [1, 2]
a = [1, 2, 3]; p a[0, 2] # => [1, 2]
a = [1, 2, 3]; a[1] = 10; p a # => [1, 10, 3] a = [[1, 2], [3, 4], [5, 6]]; a[1][0] = 10; p a # => [[1, 2], [10, 4], [5, 6]]
a = [1, 2, 3]; a[0..1] = [10, 10, 10]; p a # => [10, 10, 10, 3] a = [1, 2, 3]; a[0..1] = 10; p a # => [10, 3]
a = [1, 2, 3]; a[0, 2] = [10, 10, 10]; p a # => [10, 10, 10, 3] a = [1, 2, 3]; a[0, 2] = 10; p a # => [10, 3]
p [1, 2] + [3, 4] # => [1, 2, 3, 4]
p [1, 2] * 3 # => [1, 2, 1, 2, 1, 2]
p [1, 2, 3, 2, 2, 4, 5, 4] - [2, 4] # => [1, 3, 5]
p [1, 2, 2, 3] & [3, 2, 3, 4, 4] # => [2, 3]
p [1, 2, 2] | [3, 2, 2, 3] # => [1, 2, 3]
a = [1]; p a << 2 # => [1, 2] b = [1]; p b << 2 << 3 << 4 # => [1, 2, 3, 4]
p [1, 2, 3] <=> [-1, 1, 5] # => 1 p [1, 2, 3] <=> [1, 1, 5] # => 1 p [1, 2, 3] <=> [1, 2, 5] # => -1 p [1, 2, 3] <=> [1, 2, 3] # => 0 p [1, 2, 3] <=> [1, 2, 2] # => 1 p [1, 2, 3] <=> [1, 3, 5] # => -1 p [1, 2, 3] <=> [2, 1, 5] # => -1 p [1, 2, 3] <=> [1, 2] # => 1
p [1, 2, 3] == [1, 2, 3] # => true
p [[1, 15], [2, 25], [3, 35]].assoc(2) # => [2, 25]
a = [1, 2, 3]; p a # => [1, 2, 3] p a.at(1) # => 2 b = [["a", "b"], ["c", "d"], ["e", "f"]]; p b # => [["a", "b"], ["c", "d"], ["e", "f"]] p b.at(1) # => ["c", "d"] p b.at(1).at(0) # => "c"
data1 = [10, 30, 34, 35, 37, 44, 50, 70] p data1.bsearch { |v| v == 37 } # => 37 p data1.bsearch { |v| v <=> 37 } # => 37 data2 = [10, 30, 34, 37, 37, 38, 50, 70, 80, 100] p data2.bsearch { |v| v == 37 } # => nil p data2.bsearch { |v| v >= 37 } # => 37
a = [1, 2, 3]; p a # => [1, 2, 3] a.clear; p a # => []
p [-1, 2, -3].collect! { |v| v > 0 } # => [false, true, false]
p [1, nil, 2, 3, nil].compact # => [1, 2, 3]
p [1, 2].concat([3, 4]) # => [1, 2, 3, 4]
a = [1, 2, 2, 3]; a.delete(2); p a # => [1, 3] p [1, 2, 2, 3].delete(2) # => 2 p [1, 2, 2, 3].delete(4) # => nil p [1, 2, 2, 3].delete(4) { "Not Exist" } # => "Not Exist"
a = [1, 2, 3]; a.delete_at(1); p a # => [1, 3]
p [1, 2, 3, 4, 5].delete_if { |m| m % 2 == 0 } # => [1, 3, 5]
[10, 20, 30].each { |m| print m, " " }; print "\n" # => 10 20 30 [10, 20, 30].each_index { |m| print m, " " } # => 0 1 2
[10, 20, 30].each { |m| print m, " " }; print "\n" # => 10 20 30 [10, 20, 30].each_index { |m| print m, " " } # => 0 1 2
p [].empty? # => true
p [1, 2.0, 3] == [1, 2, 3] # => true p [1, 2.0, 3].eql?([1, 2, 3]) # => false
p [1, 2, 3][4] # => nil p [1, 2, 3].fetch(4, nil) # => nil p [1, 2, 3].fetch(4, "Not Exist") # => "Not Exist" [1, 2, 3].fetch(4) { |m| p m } # => 4 p [1, 2, 3].fetch(4) # => test.rb:5:in `fetch': index 4 out of array ・・・
a = Array.new(3); p a.fill(1) # => [1, 1, 1] a = Array.new(3); p a.fill(1, 0, 2) # => [1, 1, nil] a = Array.new(3); p a.fill(1, 0..1) # => [1, 1, nil] a = Array.new(3); p a.fill { |k| k } # => [0, 1, 2] a = Array.new(3); p a.fill(0, 2) { |k| k } # => [0, 1, nil] a = Array.new(3); p a.fill(0..1) { |k| k } # => [0, 1, nil]
p [1, 2, 3, 4, 5].first # => 1 p [1, 2, 3, 4, 5].first(2) # => [1, 2]
p [1, [2, 3], [4, 5], 6].flatten # => [1, 2, 3, 4, 5, 6]
p [1, 2, 3].include?(2) # => true
p [1, 4, 3, 4, 5].index(4) # => 1 p [1, 4, 3, 4, 5].index { |m| m > 3 } # => 1
p [1, 2, 3].insert(1, 3, 2) # => [1, 3, 2, 2, 3]
p [1, 2, 3].join("|") # => "1|2|3"
p [1, 2, 3, 4, 5].last # => 5 p [1, 2, 3, 4, 5].last(2) # => [4, 5]
p [1, 2, 3, 4, 5].length # => 5
a = [1, 2, 3, 4, 5]; a.pop; p a # => [1, 2, 3, 4]
p [1, 2, 3].push(4, 5) # => [1, 2, 3, 4, 5]
p [[1, 15], [2, 25], [3, 35]].rassoc(25) # => [2, 25]
c = Array.new(3, "a"); c[1].replace("b"); p c # => ["b", "b", "b"] b = ["a", "a", "a"]; b[1].replace("b"); p b # => ["a", "b", "a"] a = [1, 1, 1]; a.replace([1, 2, 1]); p a # => [1, 2, 1] b = [1, 1, 1]; b[1].replace(2); p b # Error
p [1, 2, 3].reverse # => [3, 2, 1]
[1, 2, 3].reverse_each { |m| print m, " " } # => 3 2 1
p [1, 4, 3, 4, 5].rindex(4) # => 3 p [1, 4, 3, 4, 5].rindex { |m| m > 3 } # => 3
a = [1, 2, 3, 4, 5]; a.shift; p a # => [2, 3, 4, 5]
a = [1, 2, 3, 4, 5]; a.slice!(0..1); p a # => [3, 4, 5]
a = [1, 2, 3, 4, 5]; a.slice!(0, 2); p a # => [3, 4, 5]
p [2, 1, 5, 3, 4].sort # => [1, 2, 3, 4, 5] p [2, 1, 5, 3, 4].sort { |a, b| a <=> b } # => [1, 2, 3, 4, 5] p [2, 1, 5, 3, 4].sort { |a, b| -a <=> -b } # => [5, 4, 3, 2, 1] p [2, 1, 5, 3, 4].sort { |a, b| (a > b) ? 1 : -1 } # => [1, 2, 3, 4, 5] p [2, 1, 5, 3, 4].sort { |a, b| (a < b) ? 1 : -1 } # => [5, 4, 3, 2, 1]
p [[1, 2], [3, 4], [5, 6]].transpose # => [[1, 3, 5], [2, 4, 6]]
p [1, 2, 2, 3, 3, 3].uniq # => [1, 2, 3]
p [3, 4, 5].unshift(1, 2) # => [1, 2, 3, 4, 5]
p [1, 2, 3, 4, 5].values_at(0, 2, 4) # => [1, 3, 5]
情報学部 | 菅沼ホーム | 目次 | 索引 |