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 programI'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