# Please debug this program

**URL:** <https://www.ruby-forum.com/t/please-debug-this-program/172695>\
**Category:** Ruby\
**Created:** [July 25, 2009, 10:02pm UTC](https://www.ruby-forum.com/t/please-debug-this-program/172695 "2009-07-25T22:02:24Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![prateekagarwal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/b38774/32.png) [@prateekagarwal](https://www.ruby-forum.com/u/prateekagarwal)\
**Post date:** [July 25, 2009, 10:02pm UTC](https://www.ruby-forum.com/t/please-debug-this-program/172695/1 "2009-07-25T22:02:24Z")

</div>

I had to make a program to estimate the value of pi using Ramanujan’s  
method.

I have made the program but it is giving the following error

Question1.rb:8:in ‘factorial’:Interrupt

where ‘factorial’ is the name of the method and Question1.rb is the file  
name.

Please reply.

---

<div class="post-metadata">

**Author:** ![prateekagarwal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/b38774/32.png) [@prateekagarwal](https://www.ruby-forum.com/u/prateekagarwal)\
**Post date:** [July 25, 2009, 11:28pm UTC](https://www.ruby-forum.com/t/please-debug-this-program/172695/2 "2009-07-25T23:28:29Z")

</div>

On Sat, Jul 25, 2009 at 4:02 PM, Prateek A.[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> I had to make a program to estimate the value of pi using Ramanujan’s  
> method.
> 
> I have made the program but it is giving the following error
> 
> Question1.rb:8:in ‘factorial’:Interrupt
> 
> where ‘factorial’ is the name of the method and Question1.rb is the file  
> name.

Try this snippet, do you see the problem here?

def add(k)  
i = 0  
loop {  
i += 1  
if (i == k) then  
return i  
else  
p i, k  
end  
sleep 1  
}  
end

k = gets  
p add(k)

---

<div class="post-metadata">

**Author:** ![prateekagarwal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/b38774/32.png) [@prateekagarwal](https://www.ruby-forum.com/u/prateekagarwal)\
**Post date:** [July 26, 2009, 1:24am UTC](https://www.ruby-forum.com/t/please-debug-this-program/172695/3 "2009-07-26T01:24:02Z")

</div>

Prateek A. wrote:

> I had to make a program to estimate the value of pi using Ramanujan’s  
> method.
> 
> I have made the program but it is giving the following error
> 
> Question1.rb:8:in ‘factorial’:Interrupt

You pressed ctrl-C?

I’m surprised it even ran at all:

def factorial(x)  
if (x==0) then  
return (1)  
end  
else  
… something else

This doesn’t make sense syntactically - an ‘else’ without an ‘if’. You  
need:

if x == 0  
return 1  
else  
… something else  
end

---

<div class="post-metadata">

**Author:** ![prateekagarwal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/b38774/32.png) [@prateekagarwal](https://www.ruby-forum.com/u/prateekagarwal)\
**Post date:** [July 26, 2009, 1:48am UTC](https://www.ruby-forum.com/t/please-debug-this-program/172695/4 "2009-07-26T01:48:55Z")

</div>

On Sat, Jul 25, 2009 at 7:24 PM, Brian C.[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> I’m surprised it even ran at all:
> 
> def factorial(x)  
> if (x==0) then  
> return (1)  
> end  
> else  
> … something else
> 
> This doesn’t make sense syntactically - an ‘else’ without an ‘if’.

An ‘else’ can live inside a method or a begin…end:

> ruby y  
> y:6: warning: else without rescue is useless  
> FOO  
> ELSE  
> FOO  
> y:3:in `foo’: FOO (RuntimeError)  
> from y:9

> cat y  
> def foo(n)  
> puts “FOO”  
> raise “FOO” if n  
> rescue  
> puts “RESCUE”  
> else  
> puts “ELSE”  
> end

foo(false)  
foo(true)

> ruby y  
> FOO  
> ELSE  
> FOO  
> RESCUE

[http://rubycentral.com/pickaxe/tut\_exceptions.html](http://rubycentral.com/pickaxe/tut_exceptions.html)  
“The else clause is a similar, although less useful, construct. If  
present, it goes after the rescue clauses and before any ensure. The  
body of an else clause is executed only if no exceptions are raised by  
the main body of code.”

---

<div class="post-metadata">

**Author:** ![prateekagarwal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/b38774/32.png) [@prateekagarwal](https://www.ruby-forum.com/u/prateekagarwal)\
**Post date:** [July 26, 2009, 6:15am UTC](https://www.ruby-forum.com/t/please-debug-this-program/172695/5 "2009-07-26T06:15:17Z")

</div>

Ugh, thanks - I’d forgotten the use of ‘else’ as part of a rescue, and  
the implicit begin/end around a method body.

> > ruby y  
> > y:6: warning: else without rescue is useless

Matz send years ago that he regretted not making -w the default.
