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.
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
Technorati Tags: First Ruby program
Blogs linking to this article
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: First Ruby program
Blogs linking to this article
5 Comments:
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.
Ashish, I appreciate your concern wrt the advt. on the blog. Rest assured that the proceeds (whatever) only go towards PuneRuby.
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
This comment has been removed by a blog administrator.
Anish, like you said I would like to answer your questions a little later, least we lose focus
Post a Comment
<< Home