facebook twitter hatena line email

「Ruby/for」の版間の差分

提供: 初心者エンジニアの簡易メモ
移動: 案内検索
(foreach)
(文字列foreach)
行14: 行14:
 
  for name in names
 
  for name in names
 
   print("name=", name)
 
   print("name=", name)
 +
end
 +
 +
==key,value==
 +
user = {:name => "taro", :age => "10", :birth => "1980-10-10"}
 +
for key, value in user
 +
    print("key=", key, " value=", value)
 
  end
 
  end

2017年11月7日 (火) 00:25時点における版

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

key,value

user = {:name => "taro", :age => "10", :birth => "1980-10-10"}
for key, value in user
   print("key=", key, " value=", value)
end