Author Topic: Ruby Error  (Read 853 times)

Okay, I'm doing the basics on this learning website.
Quote from: My Code

if 2 >= 2
    puts "If working!"
elseif 2 =< 4
    puts "Else if has arrived!"
else
    puts "Woah, it's me, else."
end

Quote from: Syntax Error
(ruby):2: syntax error, unexpected '=', expecting keyword_end
elseif 2 =< 4
          ^


Wowe this kinda reminds me of algebra

Okay, I found the acceptable code that didn't get any syntax errors.
I'm going to compare the two, see if I can find any differences.
Quote from: Correct Code
if 2 >= 3
  puts "IF"
elsif 3 <= 1
  puts "ELSEIF"
else
  puts "ELSE"
end
Edit: Can't find anything, someone care to explain?

well, in the first one, you typed "=<" instead of "<=" and judging by the syntax error thing, that was the mistake

well, in the first one, you typed "=<" instead of "<=" and judging by the syntax error thing, that was the mistake
That was it, thanks.
I'm sorry to be a pest, but I don't exactly get this one.
Quote from: Me
pain = 1
unless pain = 1
    puts "Owch!"
end
Quote from: Code Academy
Oops, try again. It looks like you didn't print anything to the console.
I don't exactly get it, here is the information on the unless statements.
http://www.codecademy.com/glossary/ruby#if-unless-elsif-and-else_unless


why did you use >= instead of just ==

why did you use >= instead of just ==
Because he was trying out the operators.

OT: The < or >always goes before the =

That was it, thanks.
I'm sorry to be a pest, but I don't exactly get this one.I don't exactly get it, here is the information on the unless statements.
http://www.codecademy.com/glossary/ruby#if-unless-elsif-and-else_unless
That's not how you use "unless".
The reason that it didn't print anything is because pain == 1. "Unless" means "If it's not" of that clears anything up.
And also = is for assigning variables. == is the operator.

Here's the fixed code.
Code: [Select]
pain = 1
unless pain == 2
    puts "Owch!"
end
or
Code: [Select]
pain = 1
unless pain != 1
    puts "Owch!"
end

both of those should print "Owch!" to the console.

Are you using CodeAcademy? I have used that too, but I stopped because I always get those confusing syntax errors. The only coding language I had interest learning was Ruby because it can be used to make anything, and because the code can even be understood by someone who isn't so tech-savvy.

Are you using CodeAcademy? I have used that too, but I stopped because I always get those confusing syntax errors. The only coding language I had interest learning was Ruby because it can be used to make anything, and because the code can even be understood by someone who isn't so tech-savvy.
I am actually using codeacdemy.
I made the script that they were telling me to make, but then I modified it :P

print "Hey-o, gimme a string please"
user_input = gets.chomp
user_input.downcase!
if user_input.include? "s"
    user_input.gsub!(/r/,"w")
else
    print "You don't have anything."
end
print "Your string is #{user_input}!"
puts ""

It replaces every "r" in their input with "w".

Okay, I have a bit of a problem.
I just downloaded the ruby program from here, but I don't know what exactly to do. If I search for things named Ruby, this is what I get.

Which one do I run?
Also, I'm looking at the bookofruby.pdf, it's pretty confusing. I'll give you guys a snippet of it.\

RUNNING RUBY PROGRAMS
It is often useful to keep a command window open in the source directory containing your Ruby program files. Assuming that the Ruby interpreter is correctly pathed on your system, you will then be able to run programs by entering ruby <program name> like this: ruby 1helloworld.rb If you are using Ruby In Steel you can run the programs in the interactive console by pressing CTRL+F5 or run them in the debugger by pressing F5.

CHAPTER ONE
Strings, Numbers, Classes and Objects
The first thing to know about the Ruby language is that it’s easy to use. To prove this, let’s look at the code of the traditional ‘Hello world’ program. Here it is: 1helloworld.rb puts 'hello world'
That’s it in its entirety. One method, puts, and one string, ‘hello world’. No headers or class definitions, no import sections or ‘main’ functions. This really is as simple as it gets. Load up the code, 1helloworld.rb, and try it out.
GETTING AND PUTTING INPUT
Having ‘put’ a string to the output (here, a command window), the obvious next step is to ‘get’ a string. As you might guess, the Ruby method for this is gets. The 2helloname.rb prompts the user for his or her name – let’s suppose it’s ‚Fred‛ - and then displays a greeting: ‚Hello Fred‛. Here is the code: 2helloname.rb print( 'Enter your name: ' ) name = gets() puts( "Hello #{name}" )
While this is still very simple, there are a few important details that need to be explained. First, notice that I’ve used print rather than puts to display the...
[/tt]

Anyways, I try the first thing it says. I'm guessing I go to "Interactive Ruby"? This is what I get.

This is cool, I'm not sure if it's the right program.
I'm going to try some code, see if it works. By the looks of it, I can't have multi-lined code.

Run ruby <file> in a terminal.

Run ruby <file> in a terminal.
I tried to run 1helloworld.rb since the book suggests I have it, this happened.

How would I create my own Ruby file?

That's not a terminal, that's the Ruby interactive interpreter.

How would I create my own Ruby file?

Any text editor? Notepad, even?