CS 3723/3721
  Programming Languages  
Ruby Lexical Level


Programs Illustrating Ruby's Lexical Level:

Fibonacci number example
C Version Long Ruby Version Normal Ruby Version
#include <stdio.h>
int main() {
   int F0 = 0, F1 = 1, F2,
      i = 0, last = 30;
   while (i < last) {
      F2 = F0 + F1;
      printf("%i\t ", F0);
      F0 = F1; F1 = F2;
      i++;
      if (i%5 == 0)
         printf("\n");
   }
   
   printf("\n");
}

#!/usr/local/bin/ruby

f0 = 0; f1 = 1;
i = 0; last = 30;
while i < last do
  f2 = f0 + f1;
  printf("%i\t ", f0);
  f0 = f1; f1 = f2;
  i += 1;
  if i%5 == 0 then
    printf("\n");
  end
end
printf("\n");
#!/usr/local/bin/ruby

f0, f1, i, last =
 0,  1, 0,  30
while i < last
  f2 = f0 + f1
  printf "%i\t ", f0
  f0, f1 = f1, f2
    i += 1
  if i%5 == 0
    printf "\n"
  end
end
printf "\n"
Common Output
0        1       1       2       3
5        8       13      21      34
55       89      144     233     377
610      987     1597    2584    4181
6765     10946   17711   28657   46368
75025    121393  196418  317811  514229


Revision date: 2004-03-28. (Please use ISO 8601, the International Standard Date and Time Notation.)