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) |
1 Comment
Christmas Wishlist:
December 12, 2011 12:17 amNote: This wishlist is “unspiritual” and material; don’t expect stuff like “family,” “world peace,” “spending time with relative xyz,” or “holiness.”
- Wii(tm)
- Super mario bros wii(tm)
- Bigger TV(Old one broke the one I’m using is tiny)
- Playstation(tm) 2
- Megaman X collection(tm) for playstation 2(tm)]
- new computer with awesome processor Intel I7(tm) sounds cool
- New linux computer for eracks(tm)
- Programming in Scala
- Erlang and OTP in Action
- Various japanese language study books
- New laptop
- New Camera!
Be first to leave a comment
Simple Egg Salad Deluxe Sandwich Three Part Recipe
December 10, 2011 6:10 amRecipe 1: Boiled Eggs
Ingredients: Egg(s), Water
- Take egg(s)
- Put in boiling water for 30 minutes
- Take out and chill in refrigerator
Recipe 2: Egg Salad
Ingredients: Boiled egg(s), Mayonnaise, Mustard
- Take boiled egg(s)
- Peel egg(s)
- Smash up in bowl with spoon
- Add mayonnaise
- Add mustard
- Take Bread
- Add egg salad
- Add leaf vegetable and cheese slice
Be first to leave a comment
Protected: On cancelled meetings, tech levels, and lonely tv
December 9, 2011 2:33 amEnter your password to view comments.
5 Quick Ways to Lose Your Youtube account
December 8, 2011 4:13 pm- Monetize a video made using someone else’s copyrighted stuff
- Advertise someone else’s product in your videos
- Sell access to your account
- Embed your videos in your ad-filled blog that has no real content other than your videos
- Hack youtube
Be first to leave a comment