Need Help with a Very, Very Simple Program

M

Max Norman

I'm almost an absolute novice in Ruby; it is my first language and I
just began learning the basics yesterday, using Chris Pine's 'Learning
to Program.'

The trouble I'm having is simple, but nonetheless I would appreciate an
explanation. The little program I've written accomplishes a simple task:
it asks for your favorite number, adds one to it, then returns it to
you. Here it is:

puts 'Hey there, I\'m Uno.'
puts 'What\'s your favorite number?'
number = gets.chomp
better = number.to_i + 1
puts better + '...That\'s better.'

I'm receiving this error: String can't be coerced into Fixnum
(TypeError).' Why can't this variable--which contains a numeric
string--be converted into an integer?
 
D

David A. Black

Hi --

I'm almost an absolute novice in Ruby; it is my first language and I
just began learning the basics yesterday, using Chris Pine's 'Learning
to Program.'

Welcome to Ruby!
The trouble I'm having is simple, but nonetheless I would appreciate an
explanation. The little program I've written accomplishes a simple task:
it asks for your favorite number, adds one to it, then returns it to
you. Here it is:

puts 'Hey there, I\'m Uno.'
puts 'What\'s your favorite number?'
number = gets.chomp
better = number.to_i + 1
puts better + '...That\'s better.'

I'm receiving this error: String can't be coerced into Fixnum
(TypeError).' Why can't this variable--which contains a numeric
string--be converted into an integer?

The reason is that better is an integer, but "That's better." is a
string. So it's like you're trying to do:

5 + "hello"

and that doesn't work. You can convert back:

puts better.to_s + "...That's better"

or use Ruby's string interpolation syntax, which inserts expressions
into strings, via the #{} construct, and does the conversion for you:

puts "#{better}...That's better."

I've interpolated better, not better.to_s, and it will automatically
be to_s'd for me.


David

--
David A. Black / Ruby Power and Light, LLC
Ruby/Rails consulting & training: http://www.rubypal.com
Now available: The Well-Grounded Rubyist (http://manning.com/black2)
Training! Intro to Ruby, with Black & Kastner, September 14-17
(More info: http://rubyurl.com/vmzN)
 
J

John W Higgins

[Note: parts of this message were removed to make it a legal post.]

...
puts 'Hey there, I\'m Uno.'
puts 'What\'s your favorite number?'
number = gets.chomp
better = number.to_i + 1
puts better + '...That\'s better.'


You're looking to the wrong line for the error

it's

puts better.to_s + ....

John
 
M

Max Norman

David said:
Hi --



Welcome to Ruby!


The reason is that better is an integer, but "That's better." is a
string. So it's like you're trying to do:

5 + "hello"

and that doesn't work. You can convert back:

puts better.to_s + "...That's better"

or use Ruby's string interpolation syntax, which inserts expressions
into strings, via the #{} construct, and does the conversion for you:

puts "#{better}...That's better."

I've interpolated better, not better.to_s, and it will automatically
be to_s'd for me.


David
Welcome to Ruby!
Thanks! I look forward to learning what appears to be an incredibly
dynamic language.


Ahh...I see. Thank you for that explanation. It's instances such as
these that allow a few basic concepts to come together.
 
J

John Barnette

Hi,

I'm almost an absolute novice in Ruby; it is my first language and I
just began learning the basics yesterday, using Chris Pine's 'Learning
to Program.'
Welcome!

The trouble I'm having is simple, but nonetheless I would appreciate an
explanation. The little program I've written accomplishes a simple task:
it asks for your favorite number, adds one to it, then returns it to
you. Here it is:

puts 'Hey there, I\'m Uno.'
puts 'What\'s your favorite number?'
number = gets.chomp
better = number.to_i + 1
puts better + '...That\'s better.'

Looks like a few other folks have already solved your problem, so a
quick note on Ruby idiom/features. Ruby lets you use single *or*
double quotes to represent a string. The difference is that double
quotes allow variables to easily be inserted in your string. Also,
they make it easy to have apostrophes in your strings! A slightly
modified example, with no more \' pain:

puts "Hey there, I'm Uno."
puts "What's your favorite number?

number = gets.chomp
better = number.to_i + 1

puts "#{better}... That's better."


~ j.
 
B

botp

....
number = gets.chomp
better = number.to_i + 1

puts "#{better}... That's better."

to the OP:

you could try playing (just for ruby fun) w the above code; make it a
one-liner, eg:

puts "#{gets.to_i + 1}... That's better."

kind regards -botp
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top