With a new day dawns another
5 minutes of Ruby tutorials! Today's silent screencast covers the following:
Classes in RubyProperty getters, setters, constructors, inheritance, you name it! We go over it in a simple example using
Donald, Justice, the
EDMUG executive and the all-important "hair" property.
Some supplements here:
- super indicates a call to the parent class
- < indicates an inheritance: e.g. "Donald < EdmugExec" means Donald inherits from EdmugExec
- @variable_name indicates an instance variable of a class
Easy reflection in RubyTake a look at how quickly we can receive class information in Ruby! Just ask and it is given.
HooksThe simulation of the EDMUG executive continues! Art imitates life as Donald finds there are just some things he can't do, so he gets Justice to do them for him...in this case, using the
method_missing hook to delegate failed requests to a different object! (This will tie in later to an interesting decorator implementation - stay tuned!)
A note here: you'll notice that method_missing is implemented using the following syntax:
def method_missing(method, *args, &block)
The * behind an argument in Ruby means that there are a variable number of arguments. The & prior to an argument in Ruby converts a block (if there is one) into a Proc object.
You can modify anything in RubyWhat happens if we want to add the infamous
swinging lightsaber method to the Donald class at run-time? What if we just want to add it to one *instance* of the Donald class? Anything is possible!!
You can modify anything in Ruby, so let's not get retardedI don't think this needs any further explanation ;)
If you somehow missed the link above,
download installment 4 right here!