LinuxQuestions.org
Help answer threads with 0 replies.
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-17-2006, 04:53 PM   #1
lmcilwain
Member
 
Registered: Dec 2003
Location: Maryland
Distribution: Fedora, Ubuntu, Centos, FreeBSD
Posts: 390

Rep: Reputation: 31
Newbie: Ruby and Writing Variables In Strings


Hello all,

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?
 
Old 10-17-2006, 09:57 PM   #2
taylor_venable
Member
 
Registered: Jun 2005
Location: Indiana, USA
Distribution: OpenBSD, Ubuntu
Posts: 892

Rep: Reputation: 43
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.)
 
Old 10-17-2006, 10:49 PM   #3
lmcilwain
Member
 
Registered: Dec 2003
Location: Maryland
Distribution: Fedora, Ubuntu, Centos, FreeBSD
Posts: 390

Original Poster
Rep: Reputation: 31
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
 
Old 10-18-2006, 10:28 AM   #4
taylor_venable
Member
 
Registered: Jun 2005
Location: Indiana, USA
Distribution: OpenBSD, Ubuntu
Posts: 892

Rep: Reputation: 43
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.
 
Old 10-18-2006, 01:19 PM   #5
lmcilwain
Member
 
Registered: Dec 2003
Location: Maryland
Distribution: Fedora, Ubuntu, Centos, FreeBSD
Posts: 390

Original Poster
Rep: Reputation: 31
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.
 
  


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
how to find duplicate strings in vertical column of strings markhod Programming 7 11-02-2005 04:04 AM
Ruby file writing NSKL Programming 0 10-30-2005 07:15 AM
BASH script – reading and writing variables to a separate file morrolan Programming 10 09-20-2005 07:45 AM
C++ converting strings to variables dhbiker Programming 9 05-31-2004 04:06 AM
Using letters/strings to assign variables in python Colonel Panic Programming 0 09-14-2001 10:13 PM

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

All times are GMT -5. The time now is 11:44 AM.

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