Ruby 配列からハッシュを生成するサンプル

環境
Windows11 pro 64bit
ruby ruby 3.0.3p157

構文
Hash(*配列名)
配列からハッシュを生成します。

使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
arr = ['keya', '123', 'keyb', '456']
h = Hash[*arr]
p h['keya']
p h['keyb']
arr = ['keya', '123', 'keyb', '456'] h = Hash[*arr] p h['keya'] p h['keyb']
arr = ['keya', '123', 'keyb', '456']

h = Hash[*arr]

p h['keya']

p h['keyb']

実行結果
“123"
“456"

Ruby

Posted by arkgame