- # Ruby入門 > Hashクラス(ハッシュ)
ハッシュの作成
スポンサーリンク
スポンサーリンク
ライフスタイル関連のコンテンツ
お金 | 仕事 | 勉強 | プライベート | 健康 | 心
プログラミング関連のコンテンツ
C言語/C++入門 | Ruby入門 | Python入門 | プログラミング全般
お金 | 仕事 | 勉強 | プライベート | 健康 | 心
プログラミング関連のコンテンツ
C言語/C++入門 | Ruby入門 | Python入門 | プログラミング全般
Rubyでは、かなり柔軟にハッシュを作成できます。
一次元ハッシュのみならず、多次元ハッシュも自由に作れます。
以下に、自在に作れるハッシュの作成の例です。
スポンサーリンク
require "pp"
hash = {
"name" => "apple",
"counts" => [10, 20, 30],
"types" => {
"f" => "fuji",
"t" => "tugaru",
"b" => "blue"
},
"color" => "red",
0 => "Kantou,Kinki,Touhoku,Kyusyu",
1 => 10000
}
p hash["name"]
p hash["counts"][1]
p hash["types"]["b"]
p hash[0].split(/,/)[2]
p hash[1]
pp hash
実行結果。
"apple"
20
"blue"
"Touhoku"
10000
{"name"=>"apple",
0=>"Kantou,Kinki,Touhoku,Kyusyu",
1=>10000,
"color"=>"red",
"types"=>{"b"=>"blue", "f"=>"fuji", "t"=>"tugaru"},
"counts"=>[10, 20, 30]}
スポンサーリンク
>> 次の記事 : ハッシュからキーと値を取り出しイテレータ処理
<< 前の記事 : ハッシュ(連想配列)
スポンサーリンク