How to run Ruby programs in MAC OS Terminal [duplicate]
Asked Answered
H

3

15

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?

Hippocras answered 16/10, 2012 at 18:2 Comment(1)
Google is your friend. The first result for "rails console" is guides.rubyonrails.org/command_line.html.Murry
Y
19

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.

Yt answered 16/10, 2012 at 18:8 Comment(6)
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:ObjectHippocras
Hard to say without seeing your code.Yt
class Hello def say puts "hello World" end endHippocras
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'Yt
Aha....Thanks sorry for the dumb question...Now its fun...Hippocras
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).Tillford
C
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.

Cutwork answered 17/10, 2012 at 16:29 Comment(0)
T
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).

Tillford answered 16/10, 2012 at 21:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.