Lesson 1: Hello World with Instant Ruby

I have been programming off and on since 1971, and when you
have jumped through all the hoops I have, in trying to get
just a simple program to compile and run in a new language,
Ruby comes as a breath of fresh air.

You always start a new language by typing a "hello world"
program. In Ruby, it is the simple line:
p "Hello World"

Running this program is a breeze. As part of the Ruby
package of applications is irb, or Instant Ruby. Go to a
console window, type irb, and you are ready to run your
first programs.

irb(main):001:0> p "hello world"
"hello world"
=> nil

To get help on any command, we have the utility ri. On the
command line, simply type "ri p" which produces
C:\ruby\InstantRails\rails_apps>ri p
p(obj, ...) => nil
For each object, directly writes obj.+inspect+ followed
by the
current output record separator to the program's
standard output.

S = Struct.new(:name, :state)
s = S['dave', 'TX']
p s


#<S name="dave", state="TX">


When programming in Ruby, it is useful to have an irb session running in a separate window, along with another window in which you run ri, so that any time you are unsure about the syntax, you can read about it in ri, and try it out in irb.

Now, for our first Rails application, let us create an application called time
we again use a console window, in which we type just two words, which produces a
flurry of activity, with many files and folders being created, which the application
tells you all about, as below:

C:\ruby\InstantRails\rails_apps>rails time
create app/controllers
create app/helpers
create app/models
create app/views/layouts
create config/environments
create components
create db
create doc
create lib
create lib/tasks
create log
create public/images
create public/javascripts
create public/stylesheets
create script/performance
create script/process
create test/fixtures
create test/functional
create test/integration
create test/mocks/development
create test/mocks/test
create test/unit
create vendor
create vendor/plugins
create tmp/sessions
create tmp/sockets
create tmp/cache
create tmp/pids
create Rakefile
create README
create app/controllers/application.rb
create app/helpers/application_helper.rb
create test/test_helper.rb
create config/database.yml
create config/routes.rb
create public/.htaccess
create config/boot.rb
create config/environment.rb
create config/environments/production.rb
create config/environments/development.rb
create config/environments/test.rb
create script/about
create script/breakpointer
create script/console
create script/destroy
create script/generate
create script/performance/benchmarker
create script/performance/profiler
create script/process/reaper
create script/process/spawner
create script/process/inspector
create script/runner
create script/server
create script/plugin
create public/dispatch.rb
create public/dispatch.cgi
create public/dispatch.fcgi
create public/404.html
create public/500.html
create public/index.html
create public/favicon.ico
create public/robots.txt
create public/images/rails.png
create public/javascripts/prototype.js
create public/javascripts/effects.js
create public/javascripts/dragdrop.js
create public/javascripts/controls.js
create public/javascripts/application.js
create doc/README_FOR_APP
create log/server.log
create log/production.log
create log/development.log
create log/test.log

What rails has done, is to set up your application for you, the way an expert would. To run it, you start the ibbuilt web server Mongrel as below:
C:\ruby\InstantRails\rails_apps\time>ruby script/server
=> Booting Mongrel (use 'script/server webrick' to force WEBrick)
=> Rails application starting on
=> Call with -d to detach
=> Ctrl-C to shutdown server
** Starting Mongrel listening at
** Starting Rails with development environment...
** Rails loaded.
** Loading any Rails specific GemPlugins
** Signals ready. INT => stop (no restart).
** Mongrel available at
** Use CTRL-C to stop.

Now, in the address bar of your browser, type and you see the front page of your web application.

This, or course, is a standard page, which isn't doing much. It  is, however, a relief to have a working application as a starting point. At any time during the development process, if the program produces an error, it is reassuring to know that you only have to go back a step to have a working program again. Now, suppose we want a web page that displays the current time. For that, we type:

C:\ruby\InstantRails\rails_apps>cd time
C:\ruby\InstantRails\rails_apps\time>ruby script/generate controller now index
exists app/controllers/
exists app/helpers/
create app/views/now
exists test/functional/
create app/controllers/now_controller.rb
create test/functional/now_controller_test.rb
create app/helpers/now_helper.rb
create app/views/now/index.html.erb

The contents of app/views/now/index.html.erb are:

<p>Find me in app/views/now/index.html.erb</p>

Using any editor, we delete these lines, replacing them with:

<%= Time.now %>

and on refreshing the page, we see:
which happens to be the time and date as I write this. Each time you refresh the page, you get the current time and date. To summarize what we did:
rails time
sets up the application
cd time
 takes us to the application root
ruby script/generate controller now index
 creates controller and view
 <%= Time.now %>
inserts into the html a Ruby expression that is evaluated at run time
In other words, with just 4 lines of text, we have created a fully functional web application that does something useful. The last two lines may need a little further explanation.

Rails contains a bunch of scripts to automate tasks that are frequently undertaken. The generate script takes as parameters:

To run the application, we run the server script, which starts a web server and tells you the IP address and port at which the server listens to requests. When a browser submits the url , the system looks for the file now_controller.rb in app/controllers and in that file finds:

class NowController < ApplicationController
def index

It creates an object of class NowController (to fully understand this, you will need to read a little about Object-Oriented Programming, for instance http://en.wikipedia.org/wiki/Object-oriented_programming , but for now just read on).

After the IP address of the server,, the first item Rails picks up is the name of the controller, "now". Then, the system expects a / followed by the name of the function within NowController that it needs to execute. Not finding any, it assumes the default function, i.e. index, executes that, then processes app/views/now/index.html.erb, converting the embedded Ruby code (found between <% and %> ) into text, and sends the result back to the browser.

Providing you with the current time and date is the least of the capabilities that the Time class provides you, in solving problems relating to date and time calculations.

As an example of time arithmetic, edit ndex.html.erb, so that it contains

An hour from now, it will be <%= Time.now + 3600 %>

Time.now represents a point in time. The number added is assumed to be seconds, and 3600 of them make an hour.

ri Time tells you more.