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.
As I am going through my ruby book on how to use flow control, one of the exercises is to create a program that sings "99 bottles of beer on the wall"
I have started to create this program and I am at the point where I want to test what I have done so far (code is not finished). When I try to run my code, I get the following error:
99bottles.rb:10: syntax error, unexpected tIDENTIFIER, expecting kEND
puts number + ' bottles of beer on the wall, ' number + ' bottles of beer. Take one down pass it around'
The snipet of code its referring to is:
puts number + ' bottles of beer on the wall, ' number + ' bottles of beer. Take one down pass it around'
My first thought is that my spacing between the variable "number" and the "+" sign are screwing things up but I can't seem how since to me that clearly looks OK.
It could also be that I just don't understand how to properly right a variable into a string, but to me that also looks right ( I even tried it with the .to_s and still nothing).
Can anyone help me understand what I am doing wrong?
The first problem is, you need a "+" operator between all string literals and variables. But after that, you'll have a problem of using a binary operator between the Fixnum variable "number" and a string literal. To fix this, make it "number.to_s" or "number.to_str" (I can't recall off-hand what the difference is, but there is one.) Alternatively, you can always embed objects and expressions within strings by enclosing them in #{...} within the literal. (This actually calls the .to_s or .to_str method on the object or expression inside the #{...}; again, I can't remember which exact method is used, but it's one of those two.)
Yeah I haven't gotten to the point in the book where it explains to me #(...) yet. Based on what its showed me I don't have much to work with .
I did see that I missed that + operator so I did add that but I am having several issues getting this to work.
I am having a hard time trying to get this program started. I can't tell what my opening loop statement should be.
Right now the code I have is:
number = 99
while 100.to_i > number.to_i
puts number.to_s + ' bottles of beer on the wall, ' + number.to_s + ' bottles of beer. Take one down pass it around'
input=gets.chomp
while input.to_i > number.to_i
if input.to_i < number.to_i
puts 'No Way! Choose a lower number then ' + number.to_s
else (haven't figure out this part yet)
end
end
end
Well, I'm not sure exactly what you're trying to do here, but you don't need to convert the variable number and the literal 100 to Integer using .to_i because they're both already of type Fixnum, which is a subclass of Integer, therefore intrinsically making both the variable number and the literal 100 treatable as Integers without any transformation necessary. That's the great thing about Ruby -- the type of a variable is whatever you put into it. Put an integer into variable x and it's type becomes Integer.
Fixnum is a subclass of Integer, and Integer is a subclass of Numeric, so any Numeric methods (like greater-than and less-than, since Numeric mixes in Comparable which supports these operations, resulting in something similar to the Haskell Ord typeclass) will also apply to Fixnums. That's how you get polymorphism into the picture, too; so you can compare Integers and Floats (both subclasses of Numeric) using the same method, inherited from their shared superclass.
Even if you don't get this all right now, I'm sure you'll discover it eventually.
Thanks Taylor, I think at this point I am probably going to start this particular task over or try something a little bit smaller like a count down of some sort and see if that helps me with my understanding.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.