LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-16-2006, 11:27 AM   #1
lmcilwain
Member
 
Registered: Dec 2003
Location: Maryland
Distribution: Fedora, Ubuntu, Centos, FreeBSD
Posts: 390

Rep: Reputation: 31
Ruby and Numbers


Hello all,

I am trying to learn Ruby and I am stuck on trying to add a number in a string. I am following a tutorial and I don't see this particular type of addition covered in the book. Can anyone help me with my problem?

The idea of this small program is to ask for a number, then recommend a higher number as the better number.

The code I have developed is as follows:

puts 'What is your favorite number?'

number = gets.chomp

puts number + ' is a nice number'

puts number + ' is a good number, but ' + number + 1 +' is a much better number'


However, the error I get is:

[lmcilwain@Blade programs]$
favoritnumber.rb:8: syntax error, unexpected tUPLUS, expecting $end
puts number + ' is a good number, but ' + number + 1.to_i +' is a much better number'


I Have tried putting .to_i at the end of the 1 so it read number + 1.to_i
or even put a .to_i at the end of the number variable in the string (not that I think I needed to) but I still don't have any luck.
 
Old 10-16-2006, 11:49 AM   #2
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 115Reputation: 115
I'm learning Ruby too. Off the bat, you are not supposed to be able to add a integer to a string. Perhaps try (untested)
Code:
puts 'What is your favorite number?'
number = gets.chomp
puts number + ' is a nice number'
puts number + ' is a good number, but ' + (number.to_i + 1).to_s +' is a much better number'
 
Old 10-16-2006, 12:20 PM   #3
lmcilwain
Member
 
Registered: Dec 2003
Location: Maryland
Distribution: Fedora, Ubuntu, Centos, FreeBSD
Posts: 390

Original Poster
Rep: Reputation: 31
I gave that a shot and I got pretty much the same error:

favoritnumber.rb:8: syntax error, unexpected tUPLUS, expecting $end
puts number + ' is a good number, but ' + (number + 1).to_s +' is a much better number'

Maybe I should be using variable to do the adding and I tried this and I still didn't have any luck. What I tried was

number = gets.chomp
betternumber = number + 1

puts number + ' is a good number, but ' + betternumber +' is a much better number'
 
Old 10-16-2006, 12:53 PM   #4
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 115Reputation: 115
Oh, try making sure there is a space on each side of the + operator.
 
Old 10-16-2006, 02:34 PM   #5
lmcilwain
Member
 
Registered: Dec 2003
Location: Maryland
Distribution: Fedora, Ubuntu, Centos, FreeBSD
Posts: 390

Original Poster
Rep: Reputation: 31
Yes there are spaces between everything pretty much.
 
Old 10-16-2006, 02:48 PM   #6
bulliver
Senior Member
 
Registered: Nov 2002
Location: Edmonton AB, Canada
Distribution: Gentoo x86_64; Gentoo PPC; FreeBSD; OS X 10.9.4
Posts: 3,760
Blog Entries: 4

Rep: Reputation: 78
Quote:
Yes there are spaces between everything pretty much.
Doubtful. This works fine:

Code:
puts number + ' is a good number, but ' + (number.to_i + 1).to_s + ' is a much better number'
However, it is better form to write as:
Code:
puts "#{number} is a good number, but #{number.to_i + 1} is a much better number"
 
Old 10-16-2006, 02:56 PM   #7
lmcilwain
Member
 
Registered: Dec 2003
Location: Maryland
Distribution: Fedora, Ubuntu, Centos, FreeBSD
Posts: 390

Original Poster
Rep: Reputation: 31
Ok I did miss a ' after the last + at the end of the last statment but I would have known that I needed to turn that number variable into an integer to get that addition.

I also don't know the significance of the # and {} around the variable number just yet. I am only on chapter 3 Hopefully my form will get better as I continue to learn this.

BTW, both ways that you have suggested worked fine.
 
Old 10-16-2006, 03:42 PM   #8
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 115Reputation: 115
Ah, the stringify sort of thing. Forgot about that. (must.. write.. more.. Ruby)
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
manipulating 64 bit numbers using 32 bit numbers. rajesh_b Programming 3 09-15-2006 09:03 AM
Portage and Ruby Ephracis Programming 3 09-15-2006 08:40 AM
sequence of numbers, how to extract which numbers are missing jonlake Programming 13 06-26-2006 03:28 AM
learn c++ before ruby? hottdogg Programming 13 06-17-2005 06:47 PM
Adding numbers, break on non-numbers... Cruger Programming 1 03-22-2004 09:18 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:55 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration