Saturday, June 24, 2006

First Ruby program

Let's start our Ruby editor SciTE. To do so, on your windows desktop click on start/Programs/Ruby/Ruby182-15/SciTE Editor. The editor window opens. Press the F8 key to open an output window. Now, click on Options/Open Global Options File and search for 'tabsize'. Edit and make tabsize=2 and indent.size=2. Press Ctrl+S and the Ctrl+W. We are now ready to write our first Ruby program.


OBJECTIVE

Ruby and Rails covered here will give you the grounding you need to understand Rails code and write your own Rails applications.


Create a folder named say rubyprograms on your C:\ We shall store all our programs in this folder. Our first program will display the string "Hello" on the command window and the name of the program will be Hello.rb

All Ruby source files have the .rb file extension. In the left window of SciTE type puts 'Hello' and then click File/Save As... Give the name Hello.rb and store it in your rubyprograms folder. Press F5 to run your program. You should see Hello in the output window on the right.

Note: puts (s in puts stands for string; puts really means put string) simply writes onto the screen whatever comes after it. In Ruby, everything from an integer to a string is considered to be an object. And each object has built in 'methods' which can be used to do various useful things. To use a method, you need to put a dot after the object, then append the method name. Some methods such as puts and gets are available everywhere and don't need to be associated with a specific object. Technically speaking, these methods are provided by Ruby's Kernel module and they are included in all Ruby objects. When you run a Ruby application, an object called main is automatically created and this object provides access to the Kernel methods (Reference: The Little Book of Ruby).

Observe:
(a) Java and C programmers - no main method/function
(b) I am using single quotes around Hello. We can use " or ' for Strings, but ' is more efficient - more on this later
(c) The Ruby standard is to use two spaces for indentation


First Post | Previous | Next


Technorati Tags:
Blogs linking to this article

8 Comments:

Blogger Ashish Kulkarni said...

Satish,

Could we do away with all these advertisements please?

Apart from that, I am all set for learning something new.

I have updated my blog stating that I have started with Ruby.

7:50 PM  
Blogger manoj sagar said...

yes i done the first hello program in ruby using sciTE editor..

7:56 PM  
Blogger SatishTalim said...

Ashish, I appreciate your concern wrt the advt. on the blog. Rest assured that the proceeds (whatever) only go towards PuneRuby.

8:51 PM  
Blogger Anish Betawadkar said...

Sir, Couple of questions:

1. When we say each objects has built in methods like puts, I can safely assume that all the objects extend kernel module?

2. Main object which is created automatically is available everywhere in the Ruby application. Is it same that "this" in java? (Not entirly but concept seems same)

3. However when I tried accessing puts using main object, "(main.puts) it gave me an error.
Hello.rb:1: undefined local variable or method `main' for main:Object (NameError)"

In case the questions are making us loose focus , let's talk about them later.

Anish

2:15 PM  
Blogger Swati said...

This comment has been removed by a blog administrator.

3:35 PM  
Anonymous satishtalim said...

Anish, like you said I would like to answer your questions a little later, least we lose focus

3:53 PM  
Blogger Abhijit Paranjpe said...

Sir,
Completed the first Ruby Program.

3:57 PM  
Blogger Scot Gardner said...

Completed

8:03 PM  

Post a Comment

Links to this post:

Create a Link

<< Home