Argh?! :) kEND, $end and kELSE in a simple program

Hi,

I’m completely new to programming and am learning using Ruby.

I’m trying to get this program working using only these basic commands
that are in my program:

puts ‘Say something to the virtual hard-of-hearing Grandpa:’
while true
said=gets.chomp
year = (1930+rand(51))
counter=0
if said==‘GOODBYE’
counter+=1
if counter==3
break
end
end
elsif said !=said.upcase
puts ‘HUH?! SAY IT LOUDER!’
counter-=1
end
else
puts ‘NO, NOT SINCE ‘+year.to_s+’!’
counter-=1
end
end

So basically it’s supposed to be that it’ll just loop forever unless you
say GOODBYE three times in a row… and not just three times with
something else in between.

Otherwise if you say anything lowcase it’ll say HUH?! […] and if you
say anything upcase it should say NOT SINCE and a random number between
1930 and 1950. I had all the basic functionality working like this:

puts ‘Say something to the virtual hard-of-hearing Grandpa:’
while true
said=gets.chomp
year = (1930+rand(21))
if said !=said.upcase
puts ‘HUH?! SAY IT LOUDER!’
else puts ‘NO, NOT SINCE ‘+year.to_s+’!’
end
end

I’m really stuck about getting ‘him’ to break after the 3 GOODBYEs in a
row, I’ve written the code the best I can and now all I get are various
combinations of kEND, $end and kELSE. I’m guessing it’s some kind of
scope issue but I don’t know enough to know what I don’t know… you
know? :slight_smile:

Can anyone see what will get ‘him’ working using just the same basic
commands that are in this program already?

Thanks in advance.

2010/1/22 Jet K. [email protected]:

Hi,

Hi !

end
elsif said !=said.upcase

You closed your ‘if’ scope too early, then the ‘elsif’ became orphan
(=> remove the ‘end’).

So basically it’s supposed to be that it’ll just loop forever unless you
say GOODBYE three times in a row… and not just three times with
something else in between.

The problem is that you reset the counter at the beginning of each
loop iteration.
“counter = 0” gets called after your program has determined what you
said (=> put it before the loop and that should do the trick ;-))

On Jan 22, 2010, at 10:33 AM, Jet K. wrote:

year = (1930+rand(51))
end

puts ‘HUH?! SAY IT LOUDER!’
scope issue but I don’t know enough to know what I don’t know… you
know? :slight_smile:

Can anyone see what will get ‘him’ working using just the same basic
commands that are in this program already?

Thanks in advance.

You have extra 'end’s where you don’t need them (see the ones I
commented out):

puts ‘Say something to the virtual hard-of-hearing Grandpa:’
while true
said=gets.chomp
year = (1930+rand(51))
counter=0
if said==‘GOODBYE’
counter+=1
if counter==3
break
end
#end
elsif said !=said.upcase
puts ‘HUH?! SAY IT LOUDER!’
counter-=1
#end
else
puts ‘NO, NOT SINCE ‘+year.to_s+’!’
counter-=1
end
end

In general, kEND or kELSE means that is a keyword either missing of
found where it shouldn’t be. And $end means the end of the file.

So if you get an error like:
“syntax error, unexpected $end, expecting kEND”

It means:
“you seem to have broken one of Ruby’s syntax rules,
I ran out of stuff in this file to look at
while I was looking for an ‘end’ keyword”

-Rob

Rob B. http://agileconsultingllc.com
[email protected]

On Jan 22, 2010, at 10:33 AM, Jet K. wrote:

year = (1930+rand(51))
end
else
puts ‘NO, NOT SINCE ‘+year.to_s+’!’
counter-=1
end
end

I’m really stuck about getting ‘him’ to break after the 3 GOODBYEs
in a
row, I’ve written the code the best I can and now all I get are
various
combinations of kEND, $end and kELSE. I’m guessing it’s some kind of
scope issue but I don’t know enough to know what I don’t know… you
know? :slight_smile:

And to explain the error from this code:

-:12: syntax error, unexpected kELSIF, expecting kEND
elsif said !=said.upcase
^
-:16: syntax error, unexpected kELSE, expecting $end

In more human terms (don’t worry, soon you’ll only see the redhead):

"I found a problem on line 12 when I saw ‘elsif’ because I was
expecting something like ‘end’ because I was inside a ‘while’ block at
the time. Here’s the point on line 12 that I was sure I had a problem.

Oh, and then on line 16 I found an ‘elsif’, but I just thought the
file would be finished by now."

-Rob

Rob B. http://agileconsultingllc.com
[email protected]

Rob B. wrote:

Thanks a lot Rob, it makes it a lot easier for me to know what Ruby is
thinking in human terms. I’m enjoying it greatly and am trying to build
up my Ruby ability to get ready to try Rails in the future. This
forum/list is truly a great resource with people like you and Xavier
willing to help the new people so readily.

And to explain the error from this code:

-:12: syntax error, unexpected kELSIF, expecting kEND
elsif said !=said.upcase
^
-:16: syntax error, unexpected kELSE, expecting $end

In more human terms (don’t worry, soon you’ll only see the redhead):

"I found a problem on line 12 when I saw ‘elsif’ because I was
expecting something like ‘end’ because I was inside a ‘while’ block at
the time. Here’s the point on line 12 that I was sure I had a problem.

Oh, and then on line 16 I found an ‘elsif’, but I just thought the
file would be finished by now."

-Rob

Rob B. http://agileconsultingllc.com
[email protected]

Xavier Noëlle wrote:

The problem is that you reset the counter at the beginning of each
loop iteration.
“counter = 0” gets called after your program has determined what you
said (=> put it before the loop and that should do the trick ;-))

Thanks very much for your help! Well, although I tried moving the
counter out of the loop (and what you said now makes perfect sense to me
that it was getting reset each time…) it now works without kENDing or
$ending ot kELSING, but when I write GOODBYE it just keeps letting me
write GOODBYE more than three times (forever actually, I would guess).

So, now I’m wondering why my counter logic isn’t working?

Judging from your error message, you might get a more helpful error
message by running ruby 1.9 (if you’re not) and/or running it ruby -w,
which gives you indentation warnings.
-r

And for everyone’s reference, my code now looks like this:

puts ‘Say something to the virtual hard-of-hearing Grandpa:’
counter=0
while true
said=gets.chomp
year = (1930+rand(51))
if said==‘GOODBYE’
counter+=1
if counter==3
break
end
elsif said !=said.upcase
puts ‘HUH?! SAY IT LOUDER!’
counter-=1
else
puts ‘NO, NOT SINCE ‘+year.to_s+’!’
counter-=1
end
end

and the problem with it is that it doesn’t break after getting GOODBYE
three times in a row…

Roger P. wrote:

you might benefit from the rbeautify gem, too
$ gem install rbeautify
$ rbeautify script_name.rb

I am grateful, I installed it and ran it and it did a good job. Anyone
have any idea why the ‘Grandpa’ won’t break though?

puts ‘Say something to the virtual hard-of-hearing Grandpa:’
counter=0
while true
said=gets.chomp
year = (1930+rand(51))
if said==‘GOODBYE’
counter+=1
if counter==3
break
end
elsif said !=said.upcase
puts ‘HUH?! SAY IT LOUDER!’
counter-=1
else
puts ‘NO, NOT SINCE ‘+year.to_s+’!’
counter-=1
end
end

you might benefit from the rbeautify gem, too
$ gem install rbeautify
$ rbeautify script_name.rb

Oops, I missed this message when I was reading the other replies.

I’m running 1.8.7 on Mac OS X 10.4 (Tiger). When I installed Macports
and then installed a newer Ruby than is on OS X 10.4 it set me up with
1.8.7. I renamed the old 10.4 ruby so it doesn’t run anymore when I type
ruby.

I’d like to run Rails, and especially Spree e-commerce.

Should both of them and all the various gems and/or plugins they require
work OK with Ruby 1.9, and if so, how do I even install it with Macports
(the only way I’m comfortable installing things) if I already asked it
for Ruby and it gave me 1.8.7… ?

Thanks again to everyone for all the help getting up and running. :slight_smile:

Roger P. wrote:

Judging from your error message, you might get a more helpful error
message by running ruby 1.9 (if you’re not) and/or running it ruby -w,
which gives you indentation warnings.
-r

On Jan 22, 2010, at 12:43 PM, Jet K. wrote:

  break

and the problem with it is that it doesn’t break after getting GOODBYE
three times in a row…

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

But it probably eventually hits the break if you keep saying “GOODBYE”

Either run the debugger or add a line right after the while true
puts “the counter is #{counter}”

Then it should become clear.

-Rob

Rob B. http://agileconsultingllc.com
[email protected]

Rob, if you wrote an “error to English” translator and submitted it as a
patch to Ruby, you would be a hero among men.

And to explain the error from this code:

-:12: syntax error, unexpected kELSIF, expecting kEND
elsif said !=said.upcase
^
-:16: syntax error, unexpected kELSE, expecting $end

In more human terms (don’t worry, soon you’ll only see the redhead):

"I found a problem on line 12 when I saw ‘elsif’ because I was
expecting something like ‘end’ because I was inside a ‘while’ block at
the time. Here’s the point on line 12 that I was sure I had a problem.

Oh, and then on line 16 I found an ‘elsif’, but I just thought the
file would be finished by now."

Rob B. wrote:

But it probably eventually hits the break if you keep saying “GOODBYE”

Either run the debugger or add a line right after the while true
puts “the counter is #{counter}”

Then it should become clear.

OK! You are absolutely correct. What was happening is that I was testing
out to make sure that the regular downcase and upcase conditions were
still working before I tried the GOODBYE again.

So, now I’ve solved the reason as to why it wasn’t breaking, but I’m
back to the drawing board about how to get it to quit after three
GOODBYEs no matter what else has already been said beforehand.

Well, I’ve got it working now. In case anyone is interested, the final
working version is:

puts ‘Say something to the virtual hard-of-hearing Grandpa:’
counter=0
while true
puts “the counter is #{counter}”
said=gets.chomp
year = (1930+rand(51))
if said==‘GOODBYE’
counter+=1
if counter==3
break
end
elsif said !=said.upcase
puts ‘HUH?! SAY IT LOUDER!’
counter=0
else
puts ‘NO, NOT SINCE ‘+year.to_s+’!’
counter=0
end
end

and my problem was I was decreasing my counter in the elsif and else
when I really should have been zeroing it.

I still don’t know about running ruby 1.9 though. I’d like to run it but
the answers to my questions below are still unknown to me. Should I
start a new thread just about these questions, or will someone let me
know here?

Above all, thanks for the help. Just the right amount of instruction
without spoiling the fun of figuring it out myself what I had done
incorrectly.

Thanks again!

Jet K. wrote:

Oops, I missed this message when I was reading the other replies.

I’m running 1.8.7 on Mac OS X 10.4 (Tiger). When I installed Macports
and then installed a newer Ruby than is on OS X 10.4 it set me up with
1.8.7. I renamed the old 10.4 ruby so it doesn’t run anymore when I type
ruby.

I’d like to run Rails, and especially Spree e-commerce.

Should both of them and all the various gems and/or plugins they require
work OK with Ruby 1.9, and if so, how do I even install it with Macports
(the only way I’m comfortable installing things) if I already asked it
for Ruby and it gave me 1.8.7… ?

Thanks again to everyone for all the help getting up and running. :slight_smile:

Roger P. wrote:

Judging from your error message, you might get a more helpful error
message by running ruby 1.9 (if you’re not) and/or running it ruby -w,
which gives you indentation warnings.
-r

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs