Forum: Ruby Pine's book "Learning to Program" - Answer?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
woodyee (Guest)
on 2006-02-28 05:36
(Received via mailing list)
Hi! I'm stuck on Chapter 5, 5.4:

"Write a program which asks for a person's favorite number. Have your
program add one to the number, then suggest the result as a bigger and
better favorite number. (Do be tactful about it, though.)".

Any ideas? I'm getting "can't convert fixnum into string" errors.
Here's one of the many things I've tried, er, failed:

puts 'What is your favorite number ?'
num = gets.chomp
better = 'num'.to_i + 1
puts 'This is better: ' + better + '.'

3rd line's getting me. Can't figure out how to add 1 to the response.
Hopefully, I'll figure this out but any tips are welcomed. Thanks!
Mando Escamilla (Guest)
on 2006-02-28 05:42
(Received via mailing list)
You're close.

better is a fixnum (like the error says).  In order to add it to the
string,
you'll need to convert it to a string.  Luckily, ruby gives you a nice
and
easy way to do it:

better.to_s will return the string version of better.

So, this makes the last line:

puts ' This is better: ' + better.to_s + '.'

Give that a shot.

--
Mando
James G. (Guest)
on 2006-02-28 05:45
(Received via mailing list)
On Feb 27, 2006, at 9:33 PM, woodyee wrote:

> num = gets.chomp
> better = 'num'.to_i + 1

There's your problem line.

'num' is a String of the letters n, u, and m.  num is the variable
holding the number.

Drop the quotes here.

> puts 'This is better: ' + better + '.'

This line will be another problem.  better will hold an Integer, but
you want a String here.  Try better.to_s instead.

> 3rd line's getting me. Can't figure out how to add 1 to the response.
> Hopefully, I'll figure this out but any tips are welcomed. Thanks!

Hope that helps.

Welcome to Ruby!

James Edward G. II
Jeff McNeil (Guest)
on 2006-02-28 05:48
(Received via mailing list)
Also, I believe  the line that that reads 'num'.to_i return zero as
you're asking the literal 'num' to return it's integer value. Try it
without those quotes.

-Jeff
Gregor K. (Guest)
on 2006-02-28 11:15
(Received via mailing list)
puts "Whats your favourite number?"
num = gets.to_i
puts "This is better: #{num+1}"


woodyee schrieb:
woodyee (Guest)
on 2006-02-28 16:10
(Received via mailing list)
Thanks! NOW, I think I get it. Here's my program:

# Write a program which asks for a person's favorite number. Have your
#program add one to the number, then suggest the result as a bigger and

#better favorite number.


puts "What is your favorite number? "
num = gets.chomp
better = num.to_i + 1
puts 'This is a better number: ' + better.to_s + '.'


Gregor's solution is compact (good thing) but it's too advanced for me
now.  :-)
What is it? Looks like a Python concocted commented out dictionary to
me...    :-)
Thanks again!
unknown (Guest)
on 2006-02-28 16:18
(Received via mailing list)
Hi --

On Tue, 28 Feb 2006, woodyee wrote:

> better = num.to_i + 1
> puts 'This is a better number: ' + better.to_s + '.'
>
>
> Gregor's solution is compact (good thing) but it's too advanced for me
> now.  :-)
> What is it? Looks like a Python concocted commented out dictionary to
> me...    :-)
> Thanks again!

Gregor is using string interpolation, to insert the expression num + 1
into the output string.  String interpolation works like this:

   "This is a string with #{2 + 2} inside it."

which will print:

   This is a string with 4 inside it.


David

--
David A. Black (removed_email_address@domain.invalid)
Ruby Power and Light (http://www.rubypowerandlight.com)

"Ruby for Rails" chapters now available
from Manning Early Access Program! http://www.manning.com/books/black
Gregor K. (Guest)
on 2006-02-28 16:29
(Received via mailing list)
1.) if you do a .to_i to num, you dont need to chomp it.
2.) if you make 'num'.to_i you try to transform the string num into
integer. it should be num.to_i, because you want the value of num and
not "num".
3.) #{expression} in string will be replaced with its value in that
string.
try

(1..10).each do |number|
   puts "Hallo there, Number #{number}!"
end

hope that helped ;)

sorry about my english, i know it sucks alot!


woodyee schrieb:
unknown (Guest)
on 2006-02-28 16:33
(Received via mailing list)
Hi --

On Tue, 28 Feb 2006, Gregor K. wrote:

> 1.) if you do a .to_i to num, you dont need to chomp it.
> 2.) if you make 'num'.to_i you try to transform the string num into integer.
> it should be num.to_i, because you want the value of num and not "num".
> 3.) #{expression} in string will be replaced with its value in that string.
> try

And of course there's always:

   puts "What's you're favorite number?"
   puts "This is better: #{gets.to_i + 1}"

:-)


David

--
David A. Black (removed_email_address@domain.invalid)
Ruby Power and Light (http://www.rubypowerandlight.com)

"Ruby for Rails" chapters now available
from Manning Early Access Program! http://www.manning.com/books/black
Gregor K. (Guest)
on 2006-02-28 16:40
(Received via mailing list)
true ;)


removed_email_address@domain.invalid schrieb:
Gregor K. (Guest)
on 2006-02-28 16:49
(Received via mailing list)
Try also this to get the feel how it works:

num = "1"
puts 'puts "num"'
puts "num"
puts 'puts num'
puts num
puts 'puts "#{num}"'
puts "#{num}"





Gregor K. schrieb:
This topic is locked and can not be replied to.