The world is full of objects. Apples. Chairs. Doorknobs. Rude People. All things, good or bad, or very bad, (I'm looking at you, rude people!) are objects.
We can interact with objects and they with us. Objects have their own characteristics, and some of them can carry out their own tasks. Apples can be delicious, some chairs can move or rock, doorknobs can turn, and rude people can suck it. Object-oriented languages, like Ruby, attempt to replicate this interaction by using objects in classes and filling them with things that describe them and things that they can do. For example, babies. Babies have characteristics and they can perform actions, so they count as objects. They can be expressed in Ruby with the following code:
class Baby
end
Boom. We now have a child. Ruby makes it easy; no labor or anything! Right now, the kid doesn't do much. He or she can only perform the very basic functions that he or she inherits as an object in Ruby. He or she isn't even a he or she yet! We can fix this by filling in the space between "Class" and "end" with the child's characteristics and actions.
We start with an initialize method that's called when an object is created. For our new child, it could look something like this:
class Baby
def initialize(eye_color, hair_color, gender, age)
@eye_color = eye-color
@hair_color = hair-color
@gender = gender
@age = age
end
end
That's a lot of initializing going on there! What we're doing is taking the information that we receive, from the arguments in the method, and setting our instance variables to their value. The instance variables are general characteristics of that particular instance of the class which any method within that class can access. So any code that we write telling the baby what it can do will be able to access those variables if it needs to.
Now that we have a kid with physical characteristics, we need to make him or her do something! Observe:
class Baby
def initialize(eye_color, hair_color, gender, age)
@eye_color = eye-color
@hair_color = hair-color
@gender = gender
@age = age
cry(4)
end
def cry(cry_level)
case cry_level
when 1
puts "Wah!"
when 2
puts "Waaaah!"
when 3
puts "WAAAAAAH WAAAAH WAAAAAH!"
else
puts "OMG I'M NEVER GOING TO STOP CRYING EVER SO IT'S POINTLESS TO TRY TO CONSOLE ME!"
end
end
end
Now our kid can do something! It can cry... Great. In our cry method, we expect one argument, an integer greater than 1, that corresponds with the amount of crying. If we look back at our initialize method, this baby is coming out kicking and screaming. Let's fix the code so we can fix the kid.
class Baby
def initialize(eye_color, hair_color, gender, age)
@eye_color = eye-color
@hair_color = hair-color
@gender = gender
@age = age
cry(4)
end
def cry(cry_level)
case cry_level
when 1
puts "Wah!"
when 2
puts "Waaaah!"
when 3
puts "WAAAAAAH WAAAAH WAAAAAH!"
else
puts "OMG I'M NEVER GOING TO STOP CRYING EVER SO IT'S POINTLESS TO TRY TO CONSOLE ME!"
end
end
def console
#Hmmmm.... I have no way of changing the cry-level from here!
end
end
Well, we've started a method to console the baby, but how can we do it? We don't have any variables that we can access to alter it's crying behavior. The cry_level in our cry method is local and can only be accessed in that method. We're going to need to make a change to our cry method and add an instance variable:
class Baby
def initialize(eye_color, hair_color, gender, age)
@eye_color = eye-color
@hair_color = hair-color
@gender = gender
@age = age
@cry_level = 4
cry
end
def cry()
case @cry_level
when 0
puts "Giggle!"
when 1
puts "Wah!"
when 2
puts "Waaaah!"
when 3
puts "WAAAAAAH WAAAAH WAAAAAH!"
else
puts "OMG I'M NEVER GOING TO STOP CRYING EVER SO IT'S POINTLESS TO TRY TO CONSOLE ME!"
end
end
def console
@cry_level -= 1
end
end
We've made cry_level an instance variable in our class Baby so that we can at last act on the level of crying and console the child. Our console method will decrease the level of crying by 1 each time it is called. Finally!
Now, I have to try one more thing...
def behave
@cry_level = 0
@behavior = "angelic"
sleep(8 hours)
end
Yes! Let's give it a try!
/Users/mbaylis/DBC Assignments/BabyClass/Baby.rb:32:in `behave': uninitialized constant InYourDreams! (GetRealError)
*sigh* Well, it was worth a shot.