「Ruby入門」ハッシュ(Hash)を使うサンプル

2020年12月20日

書式
ハッシュ名[キー]
使用例

#!/usr/bin/ruby
# -*- coding: UTF-8 -*-

cftA = {1 => "red", 2 => "green", "key02" => "AA11", "key03" => "BB22"}
p cftA[2]
p cftA["key02"]

cftB = {:apple => "A001", :orange => "B002", :grape => "C003"}
p cftB[:grape]

cftC = {keyA: "AA01", keyB: "BB02", keyC: "CC03"}
p cftC[:keyB]

実行結果
>ruby test.rb
“green"
“AA11"
“C003"
“BB02"

Ruby

Posted by arkgame