Ironruby.2.1 – Fundamentals (Class – End)

In this blog, I will wrap up the basic class concept in Ruby. So far what we have see is, how to define a class, how to create methods and how to call them. How to define instance variable (class variables) and how to access them in the code through the use of methods. Defining a method with instance variable is cool when you need to manipulate them to give a different answer. Say if you do not want to do anything with the instance variable but would like a way to access them with one or two line would be awesome, don’t you think? Well, ruby has the answer for you as well. In this final examples of the basic class, I will show you to get hold of instance variable and use them outside the class definition.

Following code is created as ‘classs7.rb’

class Person
attr :name

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

p = Person.new(“Joe”, 30, “Male”)
puts p.name

if you fireup your rbx and pass in this code like ‘rbx class7.rb’ (Remember to put .rb extension otherwise ironruby will complain it can not find the file), you will the program will return a result as follows

The value we initialized the object P with is returned successfully. This is one another way to get hold of instance variable. ‘attr’ will give you back your instance variable. But by default ‘attr’ is only getter method. It will not allow you to set the value of the name. So if you would try the following code (class8.rb)

class Person
attr :name

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

p = Person.new(“Joe”, 30, “Male”)
p.name = “John”
puts p.name

The class8.rb is same as class7.rb except in this we are trying to assign a value to objects instance variable ‘name’. As I said before, ‘attr’ is readonly you can not change the value. So if you would attempt to run this you would get the following error

As expected, As per the error, the ironruby is looking for a method called name since attr is readonly. If ‘attr’ is confusing to make the readonly instance variable, you can explicitly make the instance declare the variable as only reader not a writer. Ruby give you a way to do that as well. Look at the following code (class9.rb)

class Person
attr_reader :name

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

p = Person.new(“Joe”, 30, “Male”)
puts p.name

This is same as class7.rb where this code is more clearer for the reader, the instance variable ‘name’ is read only. So people like me will not attempt to assign any values to it. The output of this one will be same as class7.rb.

Now you might be wondering, if there is a attr_reader, then there must be a attr_writer and yes, you guessed it correct. As you would expect, attr_writer makes the instance variable write only and you can not access the value. Lets look at the following code

class Person
attr_writer :name

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

p = Person.new(“Joe”, 30, “Male”)
puts p.name

In this example (class10.rb) we made the instance variable name as write only variable outside the class and if you try to read like this code does, it would fail with ‘unable to find name’ method. So the correct way to do this in a situation where you need a way for the object to change a value of instance variable (I am not sure this happen) but you want to control the reading access to that instance variable.

In this scenario, as the following code(class11.rb) does, first create the instance variable as ‘attr_writer’ and create a method to display the instance variable, in this case, we created a method called ‘sayName’

class Person
attr_writer :name

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

def sayName
puts @name
end

end

p = Person.new(“Joe”, 30, “Male”)
p.name=”John”
p.sayName

If you run this code the result would be, the initialized name value of ‘Joe’ changed to ‘John’.

Now that you know, you can expose an instance variable with ‘attr_reader’ and ‘attr_writer’, why don’t we write a code to put them all together. ‘class12.rb’ does that for you.

class Person
attr_reader :name
attr_writer :name

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

def sayName
puts @name
end
end

p = Person.new(“Joe”, 30, “Male”)
p.name=”John”
puts p.name

The result of this code will be same as the previous example. But you might be sratching your head and trying to figure out, why the heck do I need two declaration to make my instance variable read and write and don’t worry no more. Ruby is here for the rescue. There is a verb ‘attr_accessor’ which does both for you. So if you look at the following code (class13.rb) you will accessor in action

class Person
attr_accessor :name

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

p = Person.new(“Joe”, 30, “Male”)
p.name=”John”
puts p.name

This one creates the result same as the last two code. But now the code looks very clean and we got all the pieces together.

To summaries everything we have done so far

1. It is good to put all the related things together in a class.

2. You can define a new class with ‘class nameofclass

3. By default none of the instance variable is available to the object to access.

4. You can define a method with ‘def nameofmethod‘ and end with ‘end‘.

5. Only the class methods can access the instance variable.

6. If you want to expose the instance variable then either create a explicit method or using ‘atttr’.

7. You can define a method to expose the instance variable with the same name as the instance variable. You will do this, only when you want to manipulate the value to something else.

8. If you do not want to manipulate but just want a way to expose them, you can use one of the following

  • attr – to make the instance variable readonly
  • attr_reader – same as the previous one
  • attr_writer – instance variable is available only to change.
  • attr_accessor – object read and write the instance variable

9. You can create an instance of the class by using ‘nameofclass.new’

10. If you want to initialize the instance variables during the construction, define a special method called ‘initialize’ in the class with the values you want to set.

11. You can access the methods or instance variables with a dot as ‘instancevariable.methodname

Thats it. Following picture shows most of all of it together.

Thats it folks for the basic class in ruby…..

have fun coding…

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.