the ruby object model by rafael magana

Download The Ruby Object Model by Rafael Magana

If you can't read please download the document

Upload: rafael-magana

Post on 16-May-2015

3.185 views

Category:

Technology


0 download

DESCRIPTION

Explanation of how Ruby implements OOP.

TRANSCRIPT

  • 1. Ruby The (good) compulsive obsession for the objects (or the object of obsession) An introduction to the Ruby Object Model [email protected] 1

2. What you will nd in this presentationOOP conceptsDenition of objectRuby ObjectsRuby ClassesObjects and classes interactionRuby ModulesRuby and Meta-programmingRuby VariablesRuby MethodsThe Top Level2 3. OOP concepts Inheritance It allows a class to have the same behavior as another class Ruby uses < (less than) for inheritance: class Human < Mammal The default parent class is Object Encapsulation To modify an object's state, one of the object's behaviors must be used In my_object.something, something must be a method, not a variable Polymorphism It allows two or more objects respond to the same message person.wake_up / computer.wake_up / animal.wake_up 3 4. Object? what is that?The OOP denition: Object = state + behaviorstate is described by variables (attributes)behavior is described by methodsEvery object of the same type will have its own stateAll the objects of the same type will share the same behavior4 5. Ruby Objects A Ruby object consist of the following A reference to the object immediate class (interpreter stores this in klass) A hash table of instance variables Objects does not have a table of methods, they are in the class. Only classes can have methods. A set of ags: Singleton, Mark, Finalize, Taint, Exivar, Freeze, etc.5 6. Ruby Classes (get ready to become insane)A Ruby class: its an instance object of class Class, an consists of the following:A reference to the object immediate class (interpreter stores this in klass)A hash table of instance variablesA hash table of methodsA set of ags: Singleton, Mark, Finalize, Taint, Exivar, Freeze, etc.A reference to the superclass (interpreter stores it in super) my_object.class denotes "instance of" (my_object = MyClass.new) MyClass.superclass denotes "inherits from" ( MyClass < OtherClass) In Ruby, always remember this, CLASSES ARE OBJECTS!!! 6 7. Ruby Classessince classes are objects, class objects are instances of other classbut, if classes are objects, and you just said objects doesnt havemethods, only classes does, so whats going on here?, youd say.well, I have to say that a class isnt really an object (x_X)look at the following code taken from the Ruby source but a class is still an object: * you can store instance variables * inherits from the Object class 7 8. Ruby Classes In Ruby there are two types of classes Real classes and meta-classes (singleton, virtual, Eigen or ghost classes)What is the difference? meta-classes are Real classes with a ag set to virtual a meta-class is created as needed and inserted in the inheritance chain before the object "real" class and they are hidden. What are meta-classes useful for? If the instance methods of my_object are in MyClass and MyClass is an instance of class Class, so it cant have its own instance methods, then where are the instance methods of MyClass object? The instance methods of MyClass object are in a meta-class, a Singleton class.8 9. How classes and instances interact Every method call nominate a receiver and a message. my_obj.my_method: my_obj is the receiver and my_method is the message.my_guitar = receiver; dup/play = message Guitar = receiver; strings = message 9 10. Ruby modules A Ruby module is an object that acts as a placeholder for classes, methods and constants. A module is like a class with 3 key differences It isnt a class, (but it is an object and it has instance methods), Module is actually the superclass of Class - class Class < ModuleIt cannot be instantiated (you cannot do x = MyModule.new) It can be mixed in a class to enhance its possibilities A module can be two things A namespace, a way to regroup similar things. A Mixin (mixed in), they eliminate the need of multiple inheritance 10 11. Ruby Modules As a namespace As a Mixin Kernel#requireloads an external leModule#includeModule#append_featuresit makes a reference from the class to the included module 11 12. Hierarchy diagram Kernel is a module, not a class Object class mixes in the Kernel module it denes the instance methods of Object Module inherits from ObjectUp Class inherits from Module How Ruby walks hierarchy to nd methods? Up The Out and up wayOut Out = klass = object.class Up = super = MyClass.superclass12 13. Ruby Variables Pseudo Variables (their values cant be changed with the assignment operator) self #execution context of the current method nil, true and false #sole-instances of NilClass, TrueClass and FalseClass A variable can be distinguished by the characters at the start of its name. @ = instance variable of self. @@ = class variable ^[a-z_] = local variables ^[A-Z] = constant (we can change the state of the objects they reference) ^$ = global variables13 14. Ruby Variables - selfit is only a reference to the current receiverIf you omit the receiver, it defaults to self, thecurrent object.self != this (other languages)self is a pseudo-variable, but it is still a variable,so it changes at some point, who changes thevalue of self?a method calla class/module denition 14 15. Ruby Variables - @ and @@class variables (@@)A class variable is shared among allinstances of a class and must be initializedbefore they are used.Example.class_vclass instance variables (@)aka instance variables (wrong)Example.class_instance_variableinstance variables (@)Each instance of a class has a unique set ofinstance variablesExample.new.iv 15 16. Ruby Variables - accessors accessors are Module methods []16 17. Ruby MethodsInstance methodswe dont specify the receiver, so itsself, which means the current instanceClass methodsThey dont even exist, they aresingleton methods, because they are ina singleton classIn this case we have 3 singletonmethods but Ruby only creates onesingleton class, hence the name. 17 18. Ruby Methods - object methods object-specic singleton classesObjectmessage value = String klass = String super = Object18 19. Ruby Methods - object methods object-specic singleton classes ObjectStringsuper = Objectmessage Singleton value = super = String klass = Singleton methods: /, rst 19 20. Ruby Methods - object creation obj = Object.new creates a blank object and calls the initialize method initialize method initializes the object (variables, etc) super The call to super can access a parents method regardless of its visibility, that allows the subclass to override its parents visibility rules (X_o), believe it or not.20 21. Ruby Methods - Access control Ruby denes 3 levels of protection for module and class constants and methods: Public: accessible to anyone. Methods are public by default (except for initialize, which is always private). Protected: Can be invoked only by objects of the dening class and its subclasses. Access is kept within the family. Private: Can be called only in functional form (that is, with an implicit self as the receiver). Private methods therefore can be called only in the dening class and by direct descendants within the same object. You specify access levels to methods within class or module denitions using one or more of the three functions public, protected, and private 21 22. Ruby Methods - Access control protected level:if you want one instance of a certain class to do something with another instance of its class.private levelyou dont want the instances to access some methods of its classpublic levelyou want methods to be accessed from the outside world. 22 23. Last but not least - Top level The top-level default object, main, which is an instance of Object brought into being automatically for the sole reason that something has to be self, even at the top level. A method that you dene at the top level is stored as a private instance method of the Object class. Methods dened in Object are visible to all objects23 24. The [email protected] 24