How do I create a ruby Hello world?
Solution 1:
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.
Solution 2:
You can take a look at this Ruby Programming Wiki on Wikibooks
Code:
puts 'Hello world'
Run:
$ ruby hello-world.rb
Hello world
Solution 3:
This is how to write a very simple "hello world" using Sinatra, which is a great way to bring up a Ruby-based website without using Rails. The sample is basically the same as the Sinatra folks have on the front page of their site. It's really this simple.
Install the Sinatra gem along with its dependencies:
`gem install sinatra`
Save this to a file called hi.rb
:
require 'sinatra'
get '/hi' do
"Hello World!"
end
Drop to the command-line, and enter ruby hi.rb
. After a few seconds you should see something like:
== Sinatra/1.1.0 has taken the stage on 4567 for development with backup from WEBrick
[2010-12-04 11:43:43] INFO WEBrick 1.3.1
[2010-12-04 11:43:43] INFO ruby 1.9.2 (2010-08-18) [x86_64-darwin10.5.0]
[2010-12-04 11:43:43] INFO WEBrick::HTTPServer#start: pid=37898 port=4567:
By default Sinatra serves its pages at port=4567
, but you can change it. Read the docs to learn how.
Open a new window in your browser, and go to:
http://localhost:4567/hi
and you should see Hello World!
in your browser window.
Sinatra is really easy to work with, and makes a great prototyping and light-to-medium weight MVC-like server. I love it because of its easy integration with Sequel, my favorite ORM, and HAML, which replaces ERB as the templating engine.
Sinatra's Intro doc is a great starting point. The Sinatra Book is a good resource too.
Solution 4:
How does it work in Ruby?
Ruby is a scripting language (not compiled) just like php (as you said "you have to intrepet a page") and python, bin/bash, etc...in Ruby you have libraries with helpers and very very cool stuff they are called "gems" (Ruby and Gems :D nice name convention right? BTW this is because Ruby's parent is Perl).
You can organized different files inside one Ruby's project folder, it could be in this case one *.rb file and one "Gemfile" (that's the name without extension) in which you define which "gems" you want to install in your Ruby app (read about bundler), only with this two files you will be able to successfully do anything you want but as a desktop app (by this i mean that the Ruby app you write will only be executable on a computer with Ruby installed, and you have to install it manually (with bundler so all required "gems" are in there) and then manually run Ruby's command targeting your code's main class (unless of course you create a cron-job that do this automatically for you, pretty common practice to run processes on web servers).
If you want to use Ruby to create a "webapp" , website , etc right now two pretty popular choices are using the "rails" framework and "sinatra" gem.
With rails (that's why you hear much about ruby on rails) framework you are able to execute commands to create new website project, remember that rails uses the coding pattern called MVC (model view controller) so you will have plenty options for creating your models, views and controllers individually or using "scaffold" that will create all of them for you, rails will create a bunch of files and some of them will not be *.rb of Gemfile, all of them will have a specific task: configuration files for database, labels, of config or other "gems" you install besides rails.Take in mind that rails offer stuff for TDD (test driven development) so in a matter of hours you can have a fully functional website 100% tested and operational (big infrastructure).
This is why i also brought "sinatra" gem to this conversation...sinatra will give you same functionality than rails does but instead sinatra will not install anything for you (leaving space for error if you have not expertise on setting on web servers, web apps , etc) only the sinatra framework which will run a server for you on a specific port number so that way you can then add code to your main class in order to display HTML(small infrastructure)
What is the Ruby extension like index.php for PHP?
All ruby files are using *.rb
Hope this helps!
PS: Hello world sample
- install ruby
- create a new folder an inside create a file "hello.rb"
-
open the file and add the following code:
puts 'Hello world'
close and save the file
-
now open a terminal, console, etc go to your ruby file folder path and run the following command:
ruby hello.rb
-
that will print on your console:
Hello world
Solution 5:
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.