Ruby OOP: Objects over Classes

             Aman King
       king@thoughtworks.com

          RubyConf India 2010
What is OOP?
What is a class?
What is an object?
But before the serious stuff …
Detouring through a comic book universe …




           Image copyrighted by respective owners
               Source: http://www.comics.org/
Coming back …
What is an object?
Something that takes birth …
exhibits some behavior …
interacts with other objects …
and finally dies off
          or
is simply forgotten …
all in an effort to solve a problem.
What is a class?
A container for related behavior …
that a new object can start off with.
A class itself is an object …
used to create other objects.
When creating a new object,
 a class calls the object’s
 private initialize method.
Typically a class is named …
referenced via
a global constant variable.
A class object can have singleton
      methods added to it.
These singleton methods can be
invoked wherever the class object
         is accessible …
aka
“class-level methods”.
Why use classes?
Some possible reasons
An object remembers the class
     used to create it …
explicit type checking
  using kind_of? …
implicit type checking
using case syntax …
implicit type checking
 using rescue syntax.
A class can build on other classes:
           inheritance.
A class can share state
across multiple objects:
 @@class_variables.
Some good reasons
A named container of behavior can
 have more behavior added to it.
Any added behavior becomes part
of all objects created by that class.
A class can represent
a common pattern of initialization.
Classes improve
readability and maintainability.
What a class is not?
The fundamental building block
       of an application.
A strong data type.
An enforcement of
   contract or limitations
on how objects can behave.
Ruby’s object focus
Classes are objects.
No static method-lookup tables:
      chain of ancestors.
Singleton methods.
Private access modifier
     is object-level.
Duck typing.
Finding hints in the real world …
Objects over classes:
   Consequences
Classes and hierarchies
  no longer limit you.
Think through the usage of
class-level methods and variables:
             globals?
Understanding meta-programming
           is easier.
Effective and confident
use of meta-programming.
Final words
Objects are first-class citizens.
Classes are useful but secondary.
Thank you

       king@thoughtworks.com
http://www.wikyblog.com/AmanKing

Aman kingrubyoo pnew