I need help debugging my program

I

Icarus Daedalus

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.

puts "What is your name?"
name = gets.chomp
puts "How old are you?"
age = gets.chomp
age.to_s
puts "What year is it?"
year = gets.chomp
birth = year -= age #*********************************************
puts name + " is " + age + " and was born in " + birth



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

Sebastian Hungerecker

Icarus said:
puts "How old are you?"
age = gets.chomp
age.to_s

In this line you're asking ruby to give you a string representation of age.
This is superfluous for two reasons:
a) You're not storing the result anywhere or do anything with it. That's like
if you have a line in your code that just says 2+2 and nothing else. to_s
doesn't modify the receiver it just returns a string. If you don't use the
return value, it does nothing at all.
b) gets always returns a string, so age is a string. Turning a string into a
string is redundant.

puts "What year is it?"
year = gets.chomp
birth = year -= age

Here you're trying to use the String#- method without previously defining it.
By default it's not possible in ruby to subtract strings, which is pretty
sensible, cause what would "bla" - "blubb" even mean?


HTH,
Sebastian
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top