Mark Hubbart
2/12/2005 12:17:00 AM
Hi,
On Sat, 12 Feb 2005 04:56:04 +0900, Ghelani, Vidhi
<vidhi.ghelani@intel.com> wrote:
> Hey,
>
> I could download it and also use IRB. However, I was wondering if you
> could tell me how to run this code on IRB:
> def fact(n)
> if n == 0
> 1
> else
> n * fact(n-1)
> end
> end
>
> print fact(ARGV[0].to_i), "\n"
>
> I tried doing it but its running each line separately. Is there any way
> to run the whole thing at once.
I'm not sure why you would need to run it all at once; there's no real
difference in IRB between running the statements separately and
running them all together. Is there any particular reason?
Still, you can do it if you want, by using explicit statement
separators (";") and newline escapes ("\"):
def fact(n)
if n == 0
1
else
n * fact(n-1)
end
end;print fact(ARGV[0].to_i), "\n"
This should run it as one chunk of code in irb. The ;s make sure the
interpreter separates the statements. The \s make sure it doesn't stop
evaluating the code at the newlines
As a side note, ARGV is usually empty in irb, I think.
HTH,
Mark