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!
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
···
--
NP: Katatonia - For Funerals to Come
Jabber: sepp2k@jabber.org
ICQ: 205544826
On Dec 16, 2007, at 7:48 PM, Icarus Daedalus wrote:
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