How do I create a ruby Hello world?

Viewed 87381

I know in PHP you have to intrepret a page like index.php, but in Ruby how does it work? I don't know what is the Ruby extension like index.php for PHP. Could you help me?

7 Answers

If you are talking about a command line program this will work.

puts "Hello World"

or if you want an object oriented version

class HelloWorld
   def initialize(name)
      @name = name.capitalize
   end
   def sayHi
      puts "Hello #{@name}!"
   end
end

hello = HelloWorld.new("World")
hello.sayHi

If you are looking for a ruby on rails version of Hello World. Check the Getting Started Guide for Rails.

You can take a look at this Ruby Programming Wiki on Wikibooks

Code:

puts 'Hello world'

Run:

$ ruby hello-world.rb
Hello world
puts "Hello, World!"

To run Ruby scripts on the web, you need to use a special server, run through (F)CGI, or do some other stuff; there are several ways to get different languages HTTP-accessible. However, the simplest way is probably to use a Ruby web framework, such as Ruby on Rails or Merb -- these projects include servers and all of the things you need to get going.

I know the question was talking about Ruby, but I think you meant rails (which is what it was tagged as). Rails is a web framework that uses the ruby programming language.

install rubyonrails.

Type:

rails projectname
cd projectname
ruby script/server

Navigate to http://localhost:3000

Related