Classes and class instance variables in Ruby

I have started learning Ruby..

Since one my colleagues had asked me if I know some great stuffs with Ruby on Rails.

So I have started learning curved in Ruby, and So I have discovered.. Very great features with Ruby.
It has really a very dynamic features. More specifically you have nothing to worry in the memory management.
It has a self contained garbage collection..moreover no declarations required.
And much more, it is really a kind of OOP of those variables.
Really all were treated as Object.

So that is why I have something to share on this simple code snippets I have created.

#sample Class Thing

class Thing

@@num_thingCounter = 0           #class variable same as of static variable
attr_reader :name             #attr_reader - it automatically creates an instance/class variable in a class
attr_reader :description
attr_writer :name             #attr_writer - it added the features for having an accessor methods automatically
attr_writer :description
attr_accessor(:name, :description)      #same with attr_writer only that it can handle many vars

def initialize (name, description)
@@num_thingCounter += 1
self.name = name
self.description = description
#or
@name = name
@description = description
end

def numthingCounter
@@num_thingCounter
end

end

#Instance of an object
thing = Thing.new('LLC Herbals', 'skin products')
puts thing.description
thing.name = 'LLC Supplements'
puts thing.name

puts thing.numthingCounter

thing = Thing.new('LLC Herbals1', 'skin products')
puts thing.description
thing.name = 'LLC Supplements1'
puts thing.name

puts thing.numthingCounter

#so that is it..as you have noticed it was really very dynamic isn’t it?..
This is something I really want..very programmer friendly.. ^^

Leave a Reply

Your email address will not be published. Required fields are marked *