# Re: I need help debugging my program

**URL:** <https://www.ruby-forum.com/t/re-i-need-help-debugging-my-program/123323>\
**Category:** Ruby\
**Created:** [December 16, 2007, 12:55pm UTC](https://www.ruby-forum.com/t/re-i-need-help-debugging-my-program/123323 "2007-12-16T12:55:19Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [December 16, 2007, 12:55pm UTC](https://www.ruby-forum.com/t/re-i-need-help-debugging-my-program/123323/1 "2007-12-16T12:55:19Z")

</div>

----- Original Message ----

> From: Icarus D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> To: ruby-talk ML [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> Sent: Sunday, December 16, 2007 11:48:23 AM  
> Subject: I need help debugging my program
> 
> I’m new to Ruby and I’m trying out a very basic user input program. The  
> codes below and also I’ve indicated the line giving me grief.

That is not surprising since Ruby does not automatically cast values  
like it is done in other languages.

> puts “What is your name?”  
> name = gets.chomp  
> puts “How old are you?”  
> age = gets.chomp

You may have written the above as

age = gets.chomp.to\_i

> age.to\_s

Note that does nothing since in your code age is already a string  
and you do not use the resulting value.

> puts “What year is it?”

> year = gets.chomp

add a .to\_id to get an integer.  
and since you do the same thing twice, you could:

def geti  
gets.chomp.to\_i  
end

then use geti and later improve it to detect an incorrect integer  
format.

> birth = year -= age #\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

Why do you use the -= which has a side effect on the year variable?

birth = year - age

> puts name + " is " + age + " and was born in " + birth

will not work since now you would be adding strings and integers  
together.

You could write:

puts name + " is " + age.to\_s + " and was born in " + birth.to\_s

or in more Ruby style:

puts “#{name} is #{age} and was born in #{birth}”

> Any help you could give me would be appreciated. Thanks!

You are most welcome,

Christophe
