Ironruby.2.1 – Fundamentals (Class)

As you know by Ironruby is Ruby on DLR. So if you want to learn to progra in Ironruby, you need to learn to program in Ruby first. There are plenty of resources available online to learn Ruby. Most of the time, I do not rework the same subject again to save time but in this case since we are learning it together I will go over the language with you as I learn so we can learn it together. But I would encourage you to google ruby and learn more.

So in this installment, I am planing to go over classes and how to use them in Ruby with six examples. Most importantly I take it, you know what is Object Oriented Programming(OOP). If not ,please refer to wiki for that. If I have time, may be I will come around to write that as well. but for time being, I am going to go out on a limb and assume you know OOP. If not, please leave a comment and I will surely address it.

Basic class:
We are going to create a Person class, where person has a name, age and sex. If I use this class, I should be able say the person’s name as well. So here is how we do it in ruby

class Person
def initialize(name, age, sex)
@name = name
@age = age
@sex = sex
end

def sayName
puts @name
end
end

student = Person.new(”Joe”,30,”Male”)
student.sayName()

In the above example code all the bolded words means they are reserved words in ruby. Lets walk the code and see if we can understand what it is trying to do. Most of the time the ruby code is very easy to understand if you just read it alound (to yourself).

line 1: This line declares (with the use of ‘class’) a class of Person

line 2: This line defines a funcation call of initialize, which takes three parameters namely name, age and sex. So who is calling them MUST pass all three values. This function is a special function and what is it soo special is discussed laster part of this line discussion.

line 3: Here the instance variable name (which is prefixed by @) is initialized with the value passed to initialize function called name.

line 4: It is same as line 3, except here the instance variable age initialized with age parameter passed into the function.

line 5: same as above and this time the variable is sex.

line 6: the function definition ends here. So whenever some one calls initialize function, line 3 thru 5 get executed.

line 7: blank

line 8: Defines a new function called ’sayHello’. This function does not accept any paramters.

line 9: This displays instance variable name in the console. puts method, display the display object on the console.

line 10: This ends the function definition of sayHello. So when ’sayHello’ is called it executes only line 9.

line 11: This ends the class definition of class Person.

line 12: blank

line 13: A new person instance get created. Instance name student gets created by calling Person class to create a new instance of itself.

I thought I go over little bit of OOP here. Line 1 thru 11, all it did was nothing but to define what a Person class and what it can do but not really doing anything. Did I confuse you? I am good at it :) Basically line 1 through 11 is a declaration fo Person type. In line 13, we are creating a variable of type Person using ‘new’ keyword. When ‘new’ is called, since we had the special function called ‘initialize’, this method gets called and as you can see it passes three values to the initialize method which in turn get assigned to the three class variables name, age and sex.

One important thing to note here is @ in front of the variables in line 3 thru 5. Any variable with @ in front of it is called ‘instance variable’. That means, these variables will presist the value stored in it for all the functions available in the class. For example, line 9, you can see, the code is refering to the instance variable name that get assigned in line 3. It is very important to understand this concept.

line 12: Here we are calling a function called ’sayHello’ in the Person class.

Now, how are we going to execute this code? Using your favorite editor, cut and past the code into the editor and save it as class.rb and then fireup our ironruby. At the command prompt enter ironruby class.rb and that should do the trick. This screen should look something similar to the following

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google

No Responses so far »

Comment RSS · TrackBack URI

Say your words

You must be logged in to post a comment.