for i in [1,2,3] do
puts( i )
end
for s in ['one','two','three'] do
puts( s )
end
[1,2,3].each do |i|
puts( i )
end
1
2
3
one
two
three
1
2
3
for s in ['one','two','three'] do
puts( s )
end
# ii) each
['one','two','three'].each do |s|
puts( s )
end
# --- Example 2 ---
# i) for
for x in [1, "two", [3,4,5] ] do puts( x ) end
# ii) each
[1, "two", [3,4,5] ].each do |x| puts( x ) end
one
two
three
one
two
three
1
two
3
4
5
1
two
3
4
5
# Here the 'do' keyword can be omitted
for s in ['one','two','three']
puts( s )
end
# But here it is required
for s in ['one','two','three'] do puts( s ) end
# for
for s in 1..3
puts( s )
end
# each
(1..3).each do |s|
puts(s)
end
one
two
three
one
two
three
1
2
3
1
2
3