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.

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!

Icarus D. wrote:

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

What does “birth = year -= age” try to do?

Tek

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs