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

Discussion in 'Ruby' started by Alec Williams, Jul 19, 2009.

  1. 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

    --
    Posted via http://www.ruby-forum.com/.
     
    Alec Williams, Jul 19, 2009
    #1
    1. Advertising

  2. Hi --

    On Sun, 19 Jul 2009, Alec Williams wrote:

    > 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)
     
    David A. Black, Jul 19, 2009
    #2
    1. Advertising

  3. On Sunday 19 July 2009, Alec Williams wrote:
    > |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
     
    Stefano Crocco, Jul 19, 2009
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Slain Wilde
    Replies:
    1
    Views:
    118
    Gerald Murray
    Aug 9, 2006
  2. Raju Gautam
    Replies:
    1
    Views:
    144
    Robert Klemme
    Oct 29, 2006
  3. Comfort Eagle

    unexpected kEND, expecting $

    Comfort Eagle, Nov 23, 2006, in forum: Ruby
    Replies:
    2
    Views:
    103
    Jan Svitok
    Nov 23, 2006
  4. thufir
    Replies:
    2
    Views:
    547
    thufir
    Apr 21, 2008
  5. Louise Rains
    Replies:
    4
    Views:
    479
    Louise Rains
    Aug 11, 2010
Loading...

Share This Page