Star Development Corporation

"Every man and every woman is a star" -- Liber AL vel Legis 1.3

Chapter 1: The Basics

Let's start out with the classic program, HelloWorld.

HelloWorld.rb

puts 'Hello, world!'
Now, one can type in: ruby ./HelloWorld.rb (Omit the ./ if running on a Windows machine) and get the result:
=> Hello, world!

Next, add a line to indicate to the shell that you are running a Ruby program.
The #! at the beginning of the line is called a shebang. It works in both *nix and Windows.
The -w means that you are asking the Ruby interpreter to give you warnings of non-standard usage before running your program.

#!/usr/bin/env ruby -w
puts 'Hello, world!'
This time, type in: ./HelloWorld.rb (Omit the ./ and, optionally, the .rb if running on a Windows machine):
=> Hello, world!

Now, let's put the hello statement into a method.

#!/usr/bin/env ruby -w

def hello(name)
  puts 'Hello, ' + name + '!'
end

name = 'world'
hello(name)
Next, let's put the method hello into the class Greetings.

HelloClass.rb

#!/usr/bin/env ruby -w

class Greetings
  def hello(name)
    puts 'Hello, ' + name + '!'
  end
end

g = Greetings.new
g.hello('world')
[Contents] [Next] [Home] [Links]