d

John Tippin

This Possibly Future Trendingest site around about a Multilingual Coder / Admin/ Mathmatician



Ruby Mischief

Eads 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:

Note: 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

Recipe 1: Boiled Eggs

Ingredients: Egg(s), Water

  1. Take egg(s)
  2. Put in boiling water for 30 minutes
  3. Take out and chill in refrigerator

Recipe 2:  Egg Salad

Ingredients: Boiled egg(s), Mayonnaise, Mustard

  1. Take boiled egg(s)
  2. Peel egg(s)
  3. Smash up in bowl with spoon
  4. Add mayonnaise
  5. Add mustard
Recipe 3: Egg Salad Sandwich
Ingredients: Egg Salad , Bread, Leaf vegetable (lettuce, spinach, or cabbage), Cheese Slice
  1. Take Bread
  2. Add egg salad
  3. Add leaf vegetable and cheese slice

Be first to leave a comment

Protected: On cancelled meetings, tech levels, and lonely tv

This post is password protected. To view it please enter your password below:


Enter your password to view comments.

5 Quick Ways to Lose Your Youtube account

  1. Monetize a video made using someone else’s copyrighted stuff
  2. Advertise someone else’s product in your videos
  3. Sell access to your account
  4. Embed your videos in your ad-filled blog that has no real content other than your videos
  5. Hack youtube

Be first to leave a comment


Copyright © 2011 John Tippin