Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Ruby Program Error
#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


Messages In This Thread
Ruby Program Error - by Closed - 12-12-2009, 11:40 AM
RE: Ruby Program Error (wrong forums) - by Gaijin - 12-12-2009, 12:19 PM
RE: Ruby Program Error (wrong forums) - by Jordan L. - 01-13-2010, 09:37 AM

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

Forum Jump:


Users browsing this thread: 1 Guest(s)