genie.rb:20: syntax error, unexpected $end, expecting kEND

A

Alec Williams

I get "genie.rb:20: syntax error, unexpected $end, expecting kEND" every
I try to run the above program. It's just something I did for fun to
impress freinds/family. I've looked over it many times, trying to mess
with things a little to fix it, but nothing seems to work. I'm pretty
new to the whole programming scene (I actually started yesterday) so I
probably missed something really easy but it still baffels me. Help
would be greatly appreciated for anyone who would be kind enough to give
it.

Attachments:
http://www.ruby-forum.com/attachment/3884/genie.rb
 
D

David A. Black

Hi --

I get "genie.rb:20: syntax error, unexpected $end, expecting kEND" every
I try to run the above program. It's just something I did for fun to
impress freinds/family. I've looked over it many times, trying to mess
with things a little to fix it, but nothing seems to work. I'm pretty
new to the whole programming scene (I actually started yesterday) so I
probably missed something really easy but it still baffels me. Help
would be greatly appreciated for anyone who would be kind enough to give
it.

Every if statement has to have an 'end'. Right now you've got more
if's than end's. It's because you're using this:

else
if

(with no 'end' for the if) instead of:

elsif

They're both OK, but if you use the first one, you're starting a new
if and it has to have its own end.

Compare these two:

if x
puts "x is true"
elsif y
puts "y is true"
else
puts "Neither is true"
end

if x
puts "x is true"
else
if y
puts "y is true"
else
puts "Neither is true"
end
end

You're doing something like the second, but without matching if/end.


David

--
David A. Black / Ruby Power and Light, LLC
Ruby/Rails consulting & training: http://www.rubypal.com
Now available: The Well-Grounded Rubyist (http://manning.com/black2)
Training! Intro to Ruby, with Black & Kastner, September 14-17
(More info: http://rubyurl.com/vmzN)
 
S

Stefano Crocco

|I get "genie.rb:20: syntax error, unexpected $end, expecting kEND" every
|I try to run the above program. It's just something I did for fun to
|impress freinds/family. I've looked over it many times, trying to mess
|with things a little to fix it, but nothing seems to work. I'm pretty
|new to the whole programming scene (I actually started yesterday) so I
|probably missed something really easy but it still baffels me. Help
|would be greatly appreciated for anyone who would be kind enough to give
|it.
|
|Attachments:
|http://www.ruby-forum.com/attachment/3884/genie.rb

You are missing two end before the last one. By the way, if you want to have
several "branches" in your if statement, you can use the elsif keyword inst=
ead
of nested else/if. This would make your code shorter and much easier to read
and debug. Here's how it would look:

puts 'You see a lamp on the floor of a dusty room. Do you pick it up and ru=
b it?'
lamp=3Dgets.chomp

if lamp=3D=3D'No'
puts 'You turn around, and walk out of the room, always to wonder whether=
or not you should have rubbed it...'
elsif lamp=3D=3D'Yes'
puts 'You walk up to the lamp and stroke it. Suddenly a genie bursts out =
and says "You have released me from my eternal prison. I shall now grant yo=
u one wish! State it in "I wish for (blank)" format=20
please. State it, and it shall be done."'
wish=3Dgets.chomp
if (wish=3D=3D 'I wish for world peace' or wish=3D=3D 'I wish for the end=
of world hunger')
puts 'That\'s very thoughtful of you. It shall be done!'
elsif (wish=3D=3D 'I wish for the winning lottery ticket' or wish=3D=3D '=
I wish for $1,000,000')
puts 'That is very selfish of you, but I must hold a promise. Your wish=
shall be granted.'
else
puts 'Your wish is my command.'
end
end
puts 'You walk out of the dusty room, into the sunshine, awaiting a changed=
life as a result of your recently granted wish.'

I hope this helps

Stefano
 

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,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top