# I need help debugging my program

**URL:** <https://rubytalk.org/t/i-need-help-debugging-my-program/43065>\
**Category:** ruby-talk\
**Created:** [16 December 2007 11:34 UTC](https://rubytalk.org/t/i-need-help-debugging-my-program/43065 "2007-12-16T11:34:09Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Maestroiut\_Rubytalk](https://avatars.discourse-cdn.com/v4/letter/m/85f322/32.png) [@Maestroiut\_Rubytalk](https://rubytalk.org/u/Maestroiut_Rubytalk)\
**Post date:** [16 December 2007 11:34 UTC](https://rubytalk.org/t/i-need-help-debugging-my-program/43065/1 "2007-12-16T11:34:09Z")

</div>

> From: Icarus Daedalus \<phantom3380@yahoo.co.uk\>  
> To: ruby-talk ML \<ruby-talk@ruby-lang.org\>  
> 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  
&nbsp;&nbsp;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

> **···**
>
> ----- Original Message ----
> 
> > --  
> > Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).
