[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Newbie: Ruby and Writing Variables In Strings

Lovell Mcilwain

10/18/2006 12:59:00 AM

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?

--
Posted via http://www.ruby-....

9 Answers

James Gray

10/18/2006 1:07:00 AM

0

On Oct 17, 2006, at 7:59 PM, Lovell Mcilwain wrote:

> 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

George Oliver

10/18/2006 1:10:00 AM

0

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


Lovell Mcilwain wrote:
> 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?
>
> --
> Posted via http://www.ruby-....

Lovell Mcilwain

10/18/2006 1:38:00 AM

0

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

--
Posted via http://www.ruby-....

Lovell Mcilwain

10/18/2006 1:43:00 AM

0

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)

--
Posted via http://www.ruby-....

James Gray

10/18/2006 1:46:00 AM

0

On Oct 17, 2006, at 8:37 PM, Lovell Mcilwain wrote:

> 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

Lovell Mcilwain

10/18/2006 1:58:00 AM

0

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)

--
Posted via http://www.ruby-....

Eero Saynatkari

10/18/2006 2:14:00 AM

0

On 2006.10.18 10:06, James Edward Gray II wrote:
> On Oct 17, 2006, at 7:59 PM, Lovell Mcilwain wrote:
>
> >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

Or

puts "1 + 2 = #{3}"

To avoid having to #to_s everything manually.

Lovell Mcilwain

10/18/2006 2:47:00 AM

0

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.

--
Posted via http://www.ruby-....

Eero Saynatkari

10/18/2006 3:10:00 AM

0

On 2006.10.18 11:46, Lovell Mcilwain wrote:
> 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

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

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

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