Home
1/10/20152
1/4/2015
12/28/2014
12/14/2014
12/13/2014
12/6/2014
11/30/2014
11/29/2014
11/23/2014
11/22/2014
11/15/2014
11/12/2014
11/8/2014
11/1/2014
10/31/2014

November 29
2014

Classes for the Masses

  1. class Tree
  2.   attr_accessor :energy
  3.   def initialize
  4.     @number_of_leaves = 0
  5.     @number_of_branches = 0
  6.   end
  7.   def photosynthesize(sunlight)
  8.     @energy = sunlight**@number_of_leaves
  9.   end
  10.   def eat_dirt
  11.     @energy += 1
  12.   end
  13.   def grow_branch(number)
  14.     @number_of_branches += number
  15.     @energy -= number * 10
  16.   end
  17.   def grow_leaf(number)
  18.     if @number_of_branches < 1
  19.       p "Needs branches for the leaves"
  20.     else
  21.       @number_of_leaves += number
  22.     @energy -= number
  23.   end
  24. end

In the example above I've created a Tree class. Classes are the blueprints from which objects are made. Creating an object from a class is called instantiating. New Tree objects are instantiated with the syntax:

irb(main):001:0> maple_tree = Tree.new

Every instance(object) of the Tree class will automatically have all of the methods, and within those methods are a couple different types of variables. The two types of variables I used here are local variables and instance variables.

The local variables begin with a lowercase letter or underscore, and represent a value in the method that they were created in. If I tried to access the variable called "sunlight" from outside the photosynthesize method it would not recognize it.

The instance variables are started with @ and can be accessed by any method contained within the instance of the class. This is why @number_of_leaves can be accessed outside of the initialize method, where it was first defined.

Every time I create a new tree, in addition to receiving the methods, the initialize method will automatically be called. This causes any new tree to begin with 0 leaves and 0 branches. Because of the lack of energy gathering leaves, new trees must "eat_dirt" until they have accumulated enough energy to "grow_branch" and then "grow_leaf". Trying to grow a leaf without having a branch will just be a waste of energy.

This is how the maple_tree object from the Tree class would operate.

=> maple_tree = Tree.new
=> maple_tree.eat_dirt # 1 energy
=> maple_tree.eat_dirt # 2 energy
=> maple_tree.eat_dirt # 3 energy
=> maple_tree.eat_dirt # 4 energy
=> maple_tree.eat_dirt # 5 energy
=> maple_tree.eat_dirt # 6 energy
=> maple_tree.eat_dirt # 7 energy
=> maple_tree.eat_dirt # 8 energy
=> maple_tree.eat_dirt # 9 energy
=> maple_tree.eat_dirt # 10 energy
=> maple_tree.grow_branch(1) # 0 energy (costs 10/branch)
=> maple_tree.eat_dirt # 1 energy
=> maple_tree.grow_leaf(1) # 0 energy (costs 1/leaf)
=> maple_tree.photosynthesize(10) # 10 energy (generates 10/leaf)
=> maple_tree.grow_leaf(10) # 0 energy
=> maple_tree.photosynthesize(10) # 100 energy

This is why it takes little trees so long to get big and strong.