ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
putsnumber + ' is a nice number'
putsnumber + ' 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.
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'
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'
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.