From: Dinakar Date: 2001-12-18T11:16:33+09:00 Subject: [ruby-talk:28829] Re: what is wrong with this code- never Mind figured out I figured it out. Sorry for all the trouble. Dinakar Dinakar wrote: > Hello: > > I am new to Ruby. I am just playing around and trying to write code from C into > Ruby. > Following error message is code in Ruby and then in C. Can someone tell me what > is wrong with Rubycode. C code works fine where as Ruby code give error. > Error message as follows: > move ring 1 from peg a to peg c > move ring 1 from pega to peg b > ./array_test.rb:5:in `towers': stack level too deep (SystemStackError) > from ./array_test.rb:9:in `towers' > from ./array_test.rb:9:in `towers' > from ./array_test.rb:9:in `towers' > from ./array_test.rb:9:in `towers' > from ./array_test.rb:9:in `towers' > from ./array_test.rb:9:in `towers' > from ./array_test.rb:9:in `towers' > from ./array_test.rb:9:in `towers' > ... 2693 levels... > from ./array_test.rb:9:in `towers' > from ./array_test.rb:9:in `towers' > from ./array_test.rb:9:in `towers' > from ./array_test.rb:18 > > ================ > Ruby code > #!/usr/bin/env ruby > > def towers( n, a, b, c) > @n = n.to_i > if (@n == 1) > printf "move ring %d from peg %s to peg %s\n", @n, a, c > return > end > towers((@n -1), a, c, b) > printf "move ring %d from peg%s to peg %s\n", @n, a, c > towers((@n -1), b, a, c) > return > end > n = 5 > a = 'a' > b = 'b' > c = 'c' > towers(n, a, b, c) > > ============== > C code: > #include > void towers(int n, char a, char b, char c) > { > if (n == 1) > { > printf("move ring %d from peg %c to peg %c\n", n, a, c); > return ; > } > towers(n - 1, a, c, b); > printf("Move ring %d from peg %c to peg %c\n", n, a, c); > towers(n - 1, b, a, c); > return; > } > main() > { > int n = 5; > char a= 'a'; > char b = 'c'; > char c = 'c'; > towers(n, a, b, c); > } > ================ > > Thank you. > > Dinakar > mnopq@mayo.edu