Ruby Mischief
December 13, 2011 6:06 pmEads loves python, but I like Ruby and Java.
Today, I will teach you how to add buildings to each other in Ruby.
That’s right, we’re going to add the Baxter Building (of fantastic four fame) to the Empire State Building (of tallness fame).
They will be assigned coordinates to designate their position.
First, let’s make the class.
1 2 | class Building end |
This code isn’t particularly interesting. There is nothing special about it. Let’s add the data members.
The Building class should have a name, x coordinate and y coordinate.
1 2 3 4 5 | class Building def initialize(name, x, y) @name, @x, @y =name, x, y end end |
But we want to print these suckers out (even if you don’t know we do!). Let’s add a To String method and use a puts to test it. (the Baxter Building is at coordinates 1,2 FIY)
1 2 3 4 5 6 7 8 9 10 | class Building def initialize(name, x, y) @name, @x, @y =name, x, y end def to_s "(#@name Building, #@x, #@y)" end end baxterBuilding=Building.new("Baxter",1,2) puts baxterBuilding |
Now, when people see a building, they might want to know just its name or it position. In Ruby, variables don’t seem to be accessible from the outside by default (ie, they’re not ‘public’). If we can’t access the instance variables, we won’t be able to add the buildings. So, we create accessor methods for the variables. Lets out the name of the Baxter Building to the output.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | class Building def initialize(name, x, y) @name, @x, @y =name, x, y end def to_s "(#@name Building, #@x, #@y)" end def name @name end def x @x end def y @y end end baxterBuilding=Building.new("Baxter",1,2) puts baxterBuilding puts baxterBuilding.name |
Now, suppose that Dr. Doom takes over the Baxter Building. To change the name of the Building to Doom Building we need a setter method for name. The x and y variables don’t get setters because it’s very hard to move buildings.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | class Building def initialize(name, x, y) @name, @x, @y =name, x, y end def to_s "(#@name Building, #@x, #@y)" end def name @name end def x @x end def y @y end def name=(name) @name = name end end baxterBuilding=Building.new("Baxter",1,2) puts baxterBuilding puts baxterBuilding.name #Dr. Doom takes over baxterBuilding.name="Doom" puts baxterBuilding.name |
Now is the time to add the Empire State Building to the Baxter Building. In Java, the syntax would be something like Building newBuilding = baxterBuilding.add(empireStateBuilding) . However, in ruby the syntax will look like this: newBuilding = baxterBuilding + empireStateBuilding . Do not underestimate how cool this is.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | class Building def initialize(name, x, y) @name, @x, @y =name, x, y end def to_s "(#@name Building, #@x, #@y)" end def name @name end def x @x end def y @y end def name=(name) @name = name end def +(otherBuilding) nname = @name+" / "+ otherBuilding.name nx = (@x +otherBuilding.x)/2.0 ny = (@y+ otherBuilding.y)/2.0 return Building.new(nname,nx,ny) end end baxterBuilding=Building.new("Baxter",1,2) empireStateBuilding=Building.new("Empire State",4,3) puts baxterBuilding puts empireStateBuilding newBuilding = baxterBuilding + empireStateBuilding puts(newBuilding) |
Full Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | class Building def initialize(name, x, y) @name, @x, @y =name, x, y end def to_s "(#@name Building, #@x, #@y)" end def name @name end def x @x end def y @y end def name=(name) @name = name end def +(otherBuilding) nname = @name+" / "+ otherBuilding.name nx = (@x +otherBuilding.x)/2.0 ny = (@y+ otherBuilding.y)/2.0 return Building.new(nname,nx,ny) end end baxterBuilding=Building.new("Baxter",1,2) puts baxterBuilding puts baxterBuilding.name empireStateBuilding=Building.new("Empire State",4,3) puts empireStateBuilding puts empireStateBuilding.name newBuilding = baxterBuilding + empireStateBuilding puts(newBuilding) #Dr. Doom takes over baxterBuilding.name="Doom" puts baxterBuilding.name newOtherBuilding = baxterBuilding + empireStateBuilding puts(newOtherBuilding) |
An interesting discussion is worth comment. I think that you should write more on this topic, it might not be a taboo subject but generally people are not enough to speak on such topics. To the next. Cheers