Support Forums

Full Version: Ruby Program Error
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
EDIT: This thread is really old, thanks for all the people who have helped, i was a complete Ruby Newbie .

I am sorry this is the wrong forum but look

i just started learning ruby and well, i made a Hello World!

anyways when i run it it gives me an error:

Code:
syntax error, unexpected kEND, expecting $end

anyways, here is the program:

Code:
puts 'Hello World!'
puts 'Well, this is my first program in ruby :)'
puts 'So, What is your name?'
name = gets.chomp
puts 'Very Nice Name,' + name
puts 'Well, I hope you enjoyed my Program'
puts 'Goodbye! :) '
puts ' '
end

if name == 'Ehab'
puts 'Oh, Hello Master'
puts 'Thank you for creating me'
puts 'Work Harder learning ruby, so you can make me better'
puts ' '
puts 'If you want to enter the matrix, please type the password'
pass == gets.chomp

if pass == 'ruby'
  puts 'Congratulations, You Have Cracked The Password!'
  
  else
  end

Help Please?
I don't know much about Ruby, but it looks for like you forgot to "end"

Code:
if name == 'Ehab'

Also $end means "end of file" and kEND means the "end" of an if/while... block.

edit:
LOL Ironic!
Code:
puts 'Oh, Hello Master'
I'm sorry, I know this thread is pretty old, but I couldn't bare seeing the problem not solved.

You have a few errors in your code, which I'll point and and try to explain the fix.

Here is what the Code should be:
Code:
#! /urb/bin/env ruby

puts 'Hello World!'
puts 'Well, this is my first program in ruby :)'
puts 'So, What is your name?'
name = gets.chomp
puts 'Very Nice Name,' + name
puts 'Well, I hope you enjoyed my Program'
puts 'Goodbye! :) '
puts ' '


if name == 'Ehab'
puts 'Oh, Hello Master'
puts 'Thank you for creating me'
puts 'Work Harder learning ruby, so you can make me better'
puts ' '
puts 'If you want to enter the matrix, please type the password'
pass = gets.chomp

if pass == 'ruby'
  puts 'Congratulations, You Have Cracked The Password!'
  
  end
  end

First off, you should always include:
Code:
#! /usr/bin/env ruby
This way, it will still work if someone has ruby installed on a different directory to you.

Second;
Code:
puts 'Hello World!'
puts 'Well, this is my first program in ruby :)'
puts 'So, What is your name?'
name = gets.chomp
puts 'Very Nice Name,' + name
puts 'Well, I hope you enjoyed my Program'
puts 'Goodbye! :) '
puts ' '
end
You don't need the END statement at the bottom there, there is no BEGIN, or IF statement above, so the end if un-called for.

Third:
Code:
if name == 'Ehab'
puts 'Oh, Hello Master'
puts 'Thank you for creating me'
puts 'Work Harder learning ruby, so you can make me better'
puts ' '
puts 'If you want to enter the matrix, please type the password'
pass == gets.chomp
The way you set that up isn't very clean / clear to read. Even if you type in your name, it will say Hello, that's a nice name etc.. + the next bit.
Try doing:
Code:
if name == 'Ehab'
puts 'Oh, Hello Master'
puts 'Thank you for creating me'
puts 'Work Harder learning ruby, so you can make me better'
puts ' '
puts 'If you want to enter the matrix, please type the password'
pass == gets.chomp

if name != 'Ehab'
puts 'Hello World!'
puts 'Well, this is my first program in ruby :)'
puts 'So, What is your name?'
name = gets.chomp
puts 'Very Nice Name,' + name
puts 'Well, I hope you enjoyed my Program'
puts 'Goodbye! :) '
puts ' '
So you're basically moving the top bit below your name bit. It's telling your program that if your name DOESN'T equal Ehab, then it will skip the Hello Master bit and move on to the next bit.

Fourth:
Code:
if name == 'Ehab'
puts 'Oh, Hello Master'
puts 'Thank you for creating me'
puts 'Work Harder learning ruby, so you can make me better'
puts ' '
puts 'If you want to enter the matrix, please type the password'
pass == gets.chomp
Where you have the "pass == gets.chomp" that's a simple error, you're saying if the password EQUALS what they put in.. it doesn't make any sense.
It should be:
Code:
if name == 'Ehab'
puts 'Oh, Hello Master'
puts 'Thank you for creating me'
puts 'Work Harder learning ruby, so you can make me better'
puts ' '
puts 'If you want to enter the matrix, please type the password'
pass = gets.chomp

Fifth:
Code:
if pass == 'ruby'
  puts 'Congratulations, You Have Cracked The Password!'
  
  else
  end
The if pass and what not is all right, except for the bottom. I am new to Ruby, and I'm not sure how to use the ELSE statement yet, but where you have it there is wrong. The two bottom snippets should be:
Code:
if pass == 'ruby'
  puts 'Congratulations, You Have Cracked The Password!'
  
  end
  end


I hope that helps you out and I explained it clearly enough. Keep it up with Ruby though! Smile