Newbie: Ruby and Writing Variables In Strings

L

Lovell Mcilwain

Hello all,

As I am going through my ruby book (Trying to learn Ruby of course) 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?
 
J

James Edward Gray II

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'

You are missing a plus in the above line. You need one right before
the second `number` to join it into the string.

When you get that far, you will run into a new error (assuming
`number` is actually numeric). ;)

You need to convert numbers to put them in strings. You can do this
with the to_s method available to all numbers:

puts '1 + 2 =' + 3.to_s

Hope that helps.

Welcome to Ruby.

James Edward Gray II
 
G

georgeoliverGO

Hi Lovell,

For your example,
puts number + ' bottles of beer on the wall, ' number + ' bottles of
beer. Take one down pass it around'

try this instead:

puts number + ' bottles of beer on the wall, ' + number + ' bottles of
beer. Take one down pass it around'

Note the '+ number +' at the second variable call.


best, George
 
L

Lovell Mcilwain

Thanks guys,

I did miss that + sign before the second variable. I can't believe I
missed it. I had been looking at that same piece of code for about 4
hours trying to figure it out and it was so simple :)

Now that I am still in my testing, I have run into another issue with my
while statement. I am new to programming all together so I am having a
bit of an issue understanding loops in general.

When I tried to run my program this other error has shown up:

99bottles.rb:22: syntax error, unexpected $end, expecting kEND

Its saying an unexpected end but I can't see where. My "ends" seem to
be fine but I will post the entire while loop, maybe its another simple
thing Im missing again.

number = 99

while input > number

puts number + ' bottles of beer on the wall, ' + number + ' bottles of
beer. Take one down pass it around'

input=gets.chomp

while input > number

if input < number
puts 'No Way! Choose a lower number then ' + number.to_s
else
number = (number.to_i - 1)
end
end
 
L

Lovell Mcilwain

Well looks like I didn't indent properly and I missed an end. Im going
to try and run it and see what I get (fingers crossed)
 
J

James Edward Gray II

while input > number

puts number + ' bottles of beer on the wall, ' + number + '
bottles of
beer. Take one down pass it around'

input=gets.chomp

while input > number

You have to while statements there, but only "end" one of them.
Every while needs a matching "end" where it stops.

James Edward Gray II
 
L

Lovell Mcilwain

Yep I corrected my indenting and noticed it. I did add another end to
the bottom of the program. But Im still not able to run this program.
My lack of a definition of my variable input which isn't defined till
later in a while loop.

Can anyone tell me the best way in this case to define a starting point
and explain why or how it would work? (Have to learn something with all
this)
 
E

Eero Saynatkari

--nbhonbzV/xpXxmuC
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

On Oct 17, 2006, at 7:59 PM, Lovell Mcilwain wrote:
=20
=20
You are missing a plus in the above line. You need one right before =20
the second `number` to join it into the string.
=20
When you get that far, you will run into a new error (assuming =20
`number` is actually numeric). ;)
=20
You need to convert numbers to put them in strings. You can do this =20
with the to_s method available to all numbers:
=20
puts '1 + 2 =3D' + 3.to_s

Or
=20
puts "1 + 2 =3D #{3}"

To avoid having to #to_s everything manually.

--nbhonbzV/xpXxmuC
Content-Type: application/pgp-signature
Content-Disposition: inline

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.5 (FreeBSD)

iD8DBQFFNY047Nh7RM4TrhIRAlQnAKCtpHM59w+Jn15xiLFNk9H/wuDeLwCfZD/5
/SFwH/4Iet93hYO4zX4HgkY=
=pjTr
-----END PGP SIGNATURE-----

--nbhonbzV/xpXxmuC--
 
L

Lovell Mcilwain

Converting my variables to string got my program to the point where its
asking for my input. Once I put it in, it bails on me with a comparison
error.

99 bottles of beer on the wall, 99 bottles of beer. Take one down pass
it around
98
99bottles.rb:15:in `>': comparison of String with 99 failed
(ArgumentError)
from 99bottles.rb:15

I still believe this has something to do with my starting point problem
and not knowing how to specify it correctly.
 
E

Eero Saynatkari

--IJKqlw44nclc26Ih
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

Converting my variables to string got my program to the point where its= =20
asking for my input. Once I put it in, it bails on me with a comparison= =20
error.
=20
99 bottles of beer on the wall, 99 bottles of beer. Take one down pass= =20
it around
98
99bottles.rb:15:in `>': comparison of String with 99 failed=20
(ArgumentError)
from 99bottles.rb:15

This problem is because you cannot say 99 > "6".

input =3D gets
puts 'true' if input.to_i < 99

For example. You must use explicit conversion in most cases in Ruby.

--IJKqlw44nclc26Ih
Content-Type: application/pgp-signature
Content-Disposition: inline

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.5 (FreeBSD)

iD8DBQFFNZp97Nh7RM4TrhIRAuvsAKCyppw4ocO5EPMJeWsdfDCLpujBgACgnHSS
1EUTX9Jlg1ioqWvPbONwxjQ=
=CehJ
-----END PGP SIGNATURE-----

--IJKqlw44nclc26Ih--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,143
Latest member
SterlingLa
Top