How to write a loop with a specified step in Ruby

4 Answers

0 votes
3.step(10, 2) {|n| print "#{n}, "}


#
# run:
# 
# 3, 5, 7, 9,  
#
 

 



answered Apr 10 by avibootz
0 votes
(3..10).step(2) {|n| print "#{n}, "}


#
# run:
# 
# 3, 5, 7, 9,  
#
 

 



answered Apr 10 by avibootz
0 votes
for n in (3..10).step(2)
  print "#{n}, "
end


#
# run:
# 
# 3, 5, 7, 9,  
#
 

 



answered Apr 10 by avibootz
0 votes
for n in 3.step(by: 2, to: 10)
  print "#{n}, "
end


#
# run:
# 
# 3, 5, 7, 9,  
#
 

 



answered Apr 10 by avibootz

Related questions

...