変数とはデータの入れ物。
■ソース
test = "yahoo" #文字列オブジェクト(参照値)
p container = [test,test] #出力 containerが参照しているのはtest
test[2] = 'p' #文字列オブジェクト(参照値)の配列の置き換え
p container #出力
■結果
["yahoo", "yahoo"]
["yapoo", "yapoo"]
■感想
phpやjavaをやってきたがRubyもいろいろとルールがあるものです。
■ソース
a = 1
p a
b = "str"
p b
c = [a,b,3.class(),"test"] #配列にはオブジェクトの参照値が入っている
p c
d = [a,c,[1,2,3,]]
p d
■結果
1
"str"
[1, "str", Fixnum, "test"]
[1, [1, "str", Fixnum, "test"], [1, 2, 3]]
■ソース
a = 1
p a
b = "str"
p b
c = [a,b,3.class(),"test"]
p c
d = [a,c,[1,2,3,]]
p d
p c[0]
p d[2]
p c[2 ]
p c[-3]
p c.length #配列の長さ
p c[0,4] #Cの0から4のオブジェクトを参照
p c[0..3]
p c[0...3] #端末を含まない
p c[-3..-2] #負の記号を使った参照
■結果
1
"str"
[1, "str", Fixnum, "test"]
[1, [1, "str", Fixnum, "test"], [1, 2, 3]]
1
[1, 2, 3]
Fixnum
"str"
4
[1, "str", Fixnum, "test"]
[1, "str", Fixnum, "test"]
[1, "str", Fixnum]
["str", Fixnum]
■ソース
a = [1,2] #配列に代入
p a
a[0] = 3
p a
a[4] = "4"
p a
a[0,3] = 'a','b','c'
p a
a[0,3] = 'a','b','c','d'
p a
a[1..2] = 1,2
p a
a[0,2] = "?"
p a
a[0..2] = "A"
p a
a[-1] ="z"
p a
a = ["1","2"] #配列に代入
p a
a[0] = 3
p a
a = ["1","2"] #配列に代入
p a
a[-1] = "3"
■結果
[1, 2]
[3, 2]
[3, 2, nil, nil, "4"]
["a", "b", "c", nil, "4"]
["a", "b", "c", "d", nil, "4"]
["a", 1, 2, "d", nil, "4"]
["?", 2, "d", nil, "4"]
["A", nil, "4"]
["A", nil, "z"]
["1", "2"]
[3, "2"]
["1", "2"]
■ソース
array = ["a","b","c"]
p array.length
p array.size
p array *= 2
p array.include? "c"
p array.sort
p array.uniq #重複要素を削除した配列を生成
p array
p array.uniq! #元の配列自体の重複要素を削除し更新
p array
■ソース
array = ["a","b","c"]
array.each do |item|
p item
end
array = ["a","b","c"]
array.each_with_index do |item,index|
p [item,index]
p array[index]
end
■結果
"a"
"b"
"c"
["a", 0]
"a"
["b", 1]
"b"
["c", 2]
"c"
■結果
3
3
["a", "b", "c", "a", "b", "c"]
true
["a", "a", "b", "b", "c", "c"]
["a", "b", "c"]
["a", "b", "c", "a", "b", "c"]
["a", "b", "c"]
["a", "b", "c"]
■ソース
acids = ["Adenin","Thymine","Guanine","Cytosine"]
signs = acids.map{|acid|acid[0,1]} #acid[0,1]の部分は文字列のインデックス範囲をあらわします
p signs
■結果
["A", "T", "G", "C"]
■ソース
acids = ["Adenin","Thymine","Guanine","Cytosine"]
signs = acids.map{|acid|acid.downcase}
p signs
acids = ["Adenin","Thymine","Guanine","Cytosine"]
signs = acids.map(&:downcase) #downcaseの省略記述
p signs
■結果
["adenin", "thymine", "guanine", "cytosine"]
["adenin", "thymine", "guanine", "cytosine"]
■ソース 文字列を数値に直しソート
array = ["73","2","5","1999","53"]
p array
p array.sort #普通のソート
p array.sort{|x,y| x.to_i <=> y.to_i} #数値に変換して逆普通のソート
■結果
["73", "2", "5", "1999", "53"]
["1999", "2", "5", "53", "73"]
["2", "5", "53", "73", "1999"]
■ソース 連想配列
month_to_ordinal = {
"Jan" => 1, "Fed" => 2, "Mar" => 3, "Arp" => 4, "May" => 5, "Jun" => 6, "Jul" => 7,
"Aug" => 8, "Sep" => 9, "Oct" => 10, "Nov" => 11, "Dec" => 12,
}
p month_to_ordinal["Aug"]
p month_to_ordinal["Jun"]
p month_to_ordinal["Oct"]
■結果
8
6
10
■ソース
test = "yahoo" #文字列オブジェクト(参照値)
p container = [test,test] #出力 containerが参照しているのはtest
test[2] = 'p' #文字列オブジェクト(参照値)の配列の置き換え
p container #出力
■結果
["yahoo", "yahoo"]
["yapoo", "yapoo"]
■感想
phpやjavaをやってきたがRubyもいろいろとルールがあるものです。
■ソース
a = 1
p a
b = "str"
p b
c = [a,b,3.class(),"test"] #配列にはオブジェクトの参照値が入っている
p c
d = [a,c,[1,2,3,]]
p d
■結果
1
"str"
[1, "str", Fixnum, "test"]
[1, [1, "str", Fixnum, "test"], [1, 2, 3]]
■ソース
a = 1
p a
b = "str"
p b
c = [a,b,3.class(),"test"]
p c
d = [a,c,[1,2,3,]]
p d
p c[0]
p d[2]
p c[2 ]
p c[-3]
p c.length #配列の長さ
p c[0,4] #Cの0から4のオブジェクトを参照
p c[0..3]
p c[0...3] #端末を含まない
p c[-3..-2] #負の記号を使った参照
■結果
1
"str"
[1, "str", Fixnum, "test"]
[1, [1, "str", Fixnum, "test"], [1, 2, 3]]
1
[1, 2, 3]
Fixnum
"str"
4
[1, "str", Fixnum, "test"]
[1, "str", Fixnum, "test"]
[1, "str", Fixnum]
["str", Fixnum]
■ソース
a = [1,2] #配列に代入
p a
a[0] = 3
p a
a[4] = "4"
p a
a[0,3] = 'a','b','c'
p a
a[0,3] = 'a','b','c','d'
p a
a[1..2] = 1,2
p a
a[0,2] = "?"
p a
a[0..2] = "A"
p a
a[-1] ="z"
p a
a = ["1","2"] #配列に代入
p a
a[0] = 3
p a
a = ["1","2"] #配列に代入
p a
a[-1] = "3"
■結果
[1, 2]
[3, 2]
[3, 2, nil, nil, "4"]
["a", "b", "c", nil, "4"]
["a", "b", "c", "d", nil, "4"]
["a", 1, 2, "d", nil, "4"]
["?", 2, "d", nil, "4"]
["A", nil, "4"]
["A", nil, "z"]
["1", "2"]
[3, "2"]
["1", "2"]
■ソース
array = ["a","b","c"]
p array.length
p array.size
p array *= 2
p array.include? "c"
p array.sort
p array.uniq #重複要素を削除した配列を生成
p array
p array.uniq! #元の配列自体の重複要素を削除し更新
p array
■ソース
array = ["a","b","c"]
array.each do |item|
p item
end
array = ["a","b","c"]
array.each_with_index do |item,index|
p [item,index]
p array[index]
end
■結果
"a"
"b"
"c"
["a", 0]
"a"
["b", 1]
"b"
["c", 2]
"c"
■結果
3
3
["a", "b", "c", "a", "b", "c"]
true
["a", "a", "b", "b", "c", "c"]
["a", "b", "c"]
["a", "b", "c", "a", "b", "c"]
["a", "b", "c"]
["a", "b", "c"]
■ソース
acids = ["Adenin","Thymine","Guanine","Cytosine"]
signs = acids.map{|acid|acid[0,1]} #acid[0,1]の部分は文字列のインデックス範囲をあらわします
p signs
■結果
["A", "T", "G", "C"]
■ソース
acids = ["Adenin","Thymine","Guanine","Cytosine"]
signs = acids.map{|acid|acid.downcase}
p signs
acids = ["Adenin","Thymine","Guanine","Cytosine"]
signs = acids.map(&:downcase) #downcaseの省略記述
p signs
■結果
["adenin", "thymine", "guanine", "cytosine"]
["adenin", "thymine", "guanine", "cytosine"]
■ソース 文字列を数値に直しソート
array = ["73","2","5","1999","53"]
p array
p array.sort #普通のソート
p array.sort{|x,y| x.to_i <=> y.to_i} #数値に変換して逆普通のソート
■結果
["73", "2", "5", "1999", "53"]
["1999", "2", "5", "53", "73"]
["2", "5", "53", "73", "1999"]
■ソース 連想配列
month_to_ordinal = {
"Jan" => 1, "Fed" => 2, "Mar" => 3, "Arp" => 4, "May" => 5, "Jun" => 6, "Jul" => 7,
"Aug" => 8, "Sep" => 9, "Oct" => 10, "Nov" => 11, "Dec" => 12,
}
p month_to_ordinal["Aug"]
p month_to_ordinal["Jun"]
p month_to_ordinal["Oct"]
■結果
8
6
10
0 件のコメント:
コメントを投稿