「Ruby/for」の版間の差分
提供: 初心者エンジニアの簡易メモ
(ページの作成:「 for i in 1..3 do print("i=", i) end」) |
(→key,value) |
||
(同じ利用者による、間の3版が非表示) | |||
行1: | 行1: | ||
+ | ==for== | ||
for i in 1..3 do | for i in 1..3 do | ||
print("i=", i) | print("i=", i) | ||
+ | end | ||
+ | |||
+ | ==foreach== | ||
+ | range = 1..3 | ||
+ | range.each do |i| | ||
+ | print("i=", i) | ||
+ | end | ||
+ | |||
+ | ==文字列foreach== | ||
+ | names = ["taro", "jiro", "sabu"] | ||
+ | for name in names | ||
+ | print("name=", name) | ||
+ | end | ||
+ | |||
+ | ==for(key,value)== | ||
+ | user = {:name => "taro", :age => "10", :birth => "1980-10-10"} | ||
+ | for key, value in user | ||
+ | print("key=", key, " value=", value) | ||
+ | end | ||
+ | |||
+ | ==foreach(key,value)== | ||
+ | user = {"name" => "taro", "age" => "10", "birth" => "1980-10-10"} | ||
+ | user.each do |key, value| | ||
+ | print("key=", key, " value=", value) | ||
end | end |
2017年11月7日 (火) 02:47時点における最新版
for
for i in 1..3 do print("i=", i) end
foreach
range = 1..3 range.each do |i| print("i=", i) end
文字列foreach
names = ["taro", "jiro", "sabu"] for name in names print("name=", name) end
for(key,value)
user = {:name => "taro", :age => "10", :birth => "1980-10-10"} for key, value in user print("key=", key, " value=", value) end
foreach(key,value)
user = {"name" => "taro", "age" => "10", "birth" => "1980-10-10"} user.each do |key, value| print("key=", key, " value=", value) end