Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Ruby Program Error
#1
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?
Reply
#2
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'
Reply
#3
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
At the top will be the same place you hang from.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Getting Started With Ruby eax 7 5,068 12-13-2012, 10:03 AM
Last Post: L-Carnitine
  [Tut] How to install Ruby on Mac, Windows and Linux Jordan L. 13 8,510 12-13-2012, 07:49 AM
Last Post: L-Carnitine
  Ruby eBook sockatobi 7 3,112 12-13-2012, 07:48 AM
Last Post: L-Carnitine
  what is ruby on rails DaUB3R 3 2,055 11-01-2012, 04:20 AM
Last Post: johnnymward
  Should I Start Ruby? BreShiE 13 5,823 11-13-2011, 04:14 PM
Last Post: Speedy

Forum Jump:


Users browsing this thread: 1 Guest(s)