Ruby – Just another language

I’m happy to start a thread for one of my favorite scripting language Ruby. Here you will get to know what is Ruby and how to use it. This will be considered as a very basic tutorial. But later I’ll try to cover more advanced concepts.

So, What is Ruby?

Ruby, a dynamic object-oriented language. The language is a open source which is so excited. Ruby is very simple to write, but does many complex functionality. The creator Yukihiro Matz is so creative and combined many good features from different languages and made a so exhaustive language ‘Ruby‘.

Everything is an Object

What is so special in this language. Yeah it is pure object oriented. Everything is an object. Every piece of information, code, statement, expressions and so on are objects. So Ruby is very simple and you will also feel soon when you start learning it.

Before you start Ruby, first download the installer from RubyInstaller (for Windows).

So now to ensure whether the ruby installation was successful, go to Command Prompt (Windows Key + R -> Type cmd).

Now type in your command prompt console like this:

ruby -v

The command prompt will give you the version number of the Ruby which you have installed.

IRB

IRB is an Interactive Ruby. Yeah it is very interactive. To start IRB, go to Command Prompt and type irb.

Now you are into IRB session.

irb(main):001:0>                               – This is an indicator that you are into the IRB console.

Try this,


irb(main):001:0> RUBY_VERSION

The result will be the same as ‘ruby -v’. Here ‘RUBY_VERSION’ is a constant which contains the value of the version number. In ruby, the constants are referred in capital letters.

Try this,


irb(main):002:0> total = 1+2
=> 3
irb(main):003:0> total.object_id
=> 7

You can see the instant result after your statement. Now I’ve created one variable called total. And assigned value 3 by adding 1 and 2. As I told you earlier, in ruby everything is an object, so when you call total.object_id, it gives you the real object id created.

So you want to find the class of the total object. Here 1 is a Fixnum and 2 is also a Fixnum since both are numeric values. Hence the total object is also belongs to Fixnum class. To verify this,


irb(main):004:0> total.class
=> Fixnum
irb(main):005:0> 1.class
=> Fixnum

From the above experiment, we conclude that everything in Ruby is considered as an Object of some class. Hence Ruby is most powerful, but simple Object-Oriented language.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s