Recently,I'm reading the <Ruby Way> book.Some people said that the book is suitable for someone who have some experience in Ruby and they must be right.Because there aren't any words to explain how to develop a ruby program and there are only some usage about Ruby hacks.
This blog is to blog some especial usage about OOP in Ruby.
Using Multiple Constructors
There is no real Multiple Constructors in Ruby(Because there is not override concept in ruby).But the <Ruby Way> book supplies a way to implement Multiple Constructors.In fact,this way is very like a factory pattern.The following codes is a sample of Multiple Constructors.The codes show a rectangle can have two side lengths and three color values.We create additional class methods that assume certain defaults for some of the parameters.
class ColoredRectangle
def initialize(r,g,b,s1,s2)
@r,@g,@b,@s1,@s2 = r,g,b,s1,s2
end
def ColoredRectangle.whiteRect(s1,s2)
new(0xff, 0xff, 0xff, s1, s2)
end
def ColoredRectangle.grayRect(s1, s2)
new(0x88, 0x88, 0x88, s1, s2)
end
def ColoredRectangle.coloredSquare(r, g, b, s)
new(r, g, b, s, s)
end
def ColoredRectangle.redSquare(s)
new(0xff, 0, 0, s, s)
end
end
More Elaborate Constructors
This is a very insteresting usage.When you create a complex class which need initialize a great number of instance variables in initialize methods,there are two method in ruby(but there is only one method in java except you implement all the setter methods of instance variables and call all the setter methods before you use the instance of this class).First,you can initialize all the instace variables in the initialize method and pass all the values in the arguments.Second,you can use the following way to initialize them.The way is to pass in a block to the initialize method.We can then evaluate the block in order to initialize the object.The following codes is a example about this:
class PersonalComputer
attr_accessor :manufacturer,:model,:processor,:clock,
:ram,:disk,:monitor,:colors,:vres,:hres,:net
def initialize(&block)
instance_eval █
end
end
desktop = PersonalComputer.new do
self.manufacturer = "Acme"
self.model = "THX-1138"
self.processor = "986"
self.clock = 2.4 # GHz
self.ram = 1024 # Mb
self.disk = 800 # Gb
self.monitor = 25 # inches
self.colors = 16777216
self.vres = 1280
self.hres = 1600
self.net = "T3"
end
p desktop.inspect
If you run this code ,you will get the following result:
"#<PersonalComputer:0x2bc7580 @colors=16777216, @clock=2.4, @net=\"T3\", @monitor=25, @processor=\"986\", @hres=1600, @disk=800, @model=\"THX-1138\", @vres=1280, @ram=1024, @manufacturer=\"Acme\">"
There are several things should be noted:
First of all,we're using accessors for our attributes so that we can assign values to them in an intuitive way.Second,the reference to self is necessary because a setter always takes an explicit receiver to distinguish the method call from an ordinary assignment to a local variable.Third,we should use instance_eval instead of eval in order to evaluate the block in the context of the object rather than that of the caller.