Ruby 多重代入を行う

Rubyで、変数を一括で代入できる多重代入を行うサンプルコードを記述してます。rubyのバージョンは2.7.2を使用してます。
環境
- OS windows10 pro 64bit
- ruby 2.7.2p137
多重代入
rubyは、複数の変数に値を代入する際に、通常、以下のように代入しますが、
str1 = "hello"
str2 = "world"
str3 = "!!"
「,」を使用して、1行で記述することが可能です。
str1, str2, str3 = "hello", "world", "!!"
p str1 # "hello"
p str2 # "world"
p str3 # "!!"
配列を使用して、代入することも可能です。
str1, str2, str3 = ["hello", "world", "!!"]
p str1 # "hello"
p str2 # "world"
p str3 # "!!"
パフォーマンスは、1つずつ代入したほうが良さそうです。
【参考】 それぞれの方法で、1000万回変数を代入した結果
user system total real
#通常 0.859000 0.000000 0.859000 ( 0.861680)
#多重代入 1.125000 0.000000 1.125000 ( 1.125333)
#配列 1.125000 0.000000 1.125000 ( 1.126306)
-
前の記事
TortoiseGitを使ってファイル名の変更を行う 2021.11.05
-
次の記事
SourceTree タグを削除する 2021.11.05
コメントを書く