A.L A.L - 1 year ago 74
Ruby Question

Translation of a C for loop to Ruby

I have this code:

int i, n=3;
for (i = 1; i <= n; i++){
printf("i = %d\n", i);
i = 3

I know that it will go through the code 2 times. How do you translate this code to Ruby or how would this look like in Ruby? I have done some researcchh on it and since I am extremely new to Ruby I have no clue. The inside print part doesn't really matter I am more worried about how to set it up.

Answer Source

Here's a literal translation into Ruby of your code.

n = 3
i = 1
while i <= n
  puts "i = #{i}"
  i = 3