ruby for
for i in 0..2 do
print(i)
end
# => 0
# => 1
# => 2
3.33
3
while $i < $num do
puts("Inside the loop i = #$i" )
$i +=1
end
Thank you!
3
0
0
6
for element in array do
element.do_stuff
end
Thank you!
6
0
3.88
8
for counter in 1..5
puts "iteration #{counter}"
end
#=> iteration 1
#=> iteration 2
#=> iteration 3
#=> iteration 4
#=> iteration 5
Thank you!
8
0
4.2
10
# You can use an each to mimic the functionality of a for loop
(0..5).each do |i|
puts "Inside loop i = #{i}"
# put your code for loop here
end
# outputs
# Inside loop i = 0
# Inside loop i = 1
# Inside loop i = 2
# Inside loop i = 3
# Inside loop i = 4
# Inside loop i = 5
Thank you!
10
0
Are there any code examples left?
New code examples in category Ruby
-
Ruby 2022-03-27 13:25:03 ruby assign value to hash
-
Ruby 2022-03-25 04:05:10 test if array empty ruby
-
Ruby 2022-03-24 19:45:17 rails update without validation
-
Ruby 2022-03-23 05:15:08 ruby hello world
-
Ruby 2022-03-23 02:40:01 rails keep all params except for some
-
Ruby 2022-03-20 06:15:08 rails hidden field default value
-
Ruby 2022-03-19 19:55:31 print things ruby
-
Ruby 2022-03-18 22:15:07 rails command line
-
Ruby 2022-03-18 08:10:03 ruby get the number of same element in array
-
Ruby 2022-03-17 02:40:17 drop rails all db