14

Possible Duplicate:
How to run ruby files?

I am starting to learn Ruby and having a hard time running the Ruby classes in the Terminal.

I created a class in the Sublime Text editor, just "hello world". I can compile using ruby hello.rb, but how do I execute it?

I went to the terminal in my root directory and typed rails c which gave me a console. Could some one please tell me how to create an instance? Which console do I use?

Community
  • 1
  • 1
Ducati007
  • 265
  • 1
  • 5
  • 14
  • Google is your friend. The first result for "rails console" is http://guides.rubyonrails.org/command_line.html. – Thilo Oct 19 '12 at 17:12

3 Answers3

18

Ruby is interpreted, so you don't need to worry about a separate compile step. ruby hello.rb is the execution command.

The standard interactive shell (REPL) is irb.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
pje
  • 21,801
  • 10
  • 54
  • 70
  • Yes after I typed ruby hello.rb it gave me nothing so no errors.After that I tried getting below error in irb 1.9.3-p194 :003 > h = Hello.new() NameError: undefined local variable or method `hello' for main:Object – Ducati007 Oct 16 '12 at 18:12
  • Hard to say without seeing your code. – pje Oct 16 '12 at 18:15
  • class Hello def say puts "hello World" end end – Ducati007 Oct 16 '12 at 18:18
  • 1
    irb doesn't know about the class `Hello` unless you tell it. If you run `irb` from the same directory, you can require the file from within irb using `require './hello.rb'` – pje Oct 16 '12 at 18:35
  • Aha....Thanks sorry for the dumb question...Now its fun... – Ducati007 Oct 16 '12 at 18:44
  • pje's answer is true in IRB, but I should point out that this `require` won't work inside a *script* in Ruby 1.9 or above; in that case, you'd need to use `require_relative 'hello'` (with or without the `.rb` extension). – echristopherson Oct 16 '12 at 21:36
1

I think, this is very simple task. Paste in terminal ruby <your script name>.rb

This is all. Ruby is interpreted lang. Compiler doesn`t exist at all. Only interpreter. I use Ruby only few times, but I think, you must run your method hello. Your code only create the class and nothing else. You should firstly learn Ruby and then RoR.

Demagog
  • 51
  • 3
0

As others have pointed out, running ruby hello.rb does run the script; there is no compilation involved (except behind the scenes in the Ruby virtual machine, but you don't need to concern yourself with that).

Based on the code of the file which you gave in a comment (I've put in line breaks and indentation):

class Hello
  def say
    puts "hello World"
  end
end

... the reason your script doesn't seem to do anything is that it only defines a class and a method but doesn't instantiate the class or call the method. You had the right idea (in another comment) to call h = Hello.new(); after that you can put h.say and it will say "hello World".

(Parentheses are usually not required, including in these two method calls; but sometimes they are important. There are varying conventions, but most Rubyists skip them when calling methods without any arguments, like new and say here.)

EDIT: rails c is for Ruby on Rails, which is a separate entity from the Ruby language (although it's written in Ruby).

echristopherson
  • 6,974
  • 2
  • 21
  • 31