<<

Modules and Classes

Venkat Subramaniam – [email protected] 1

Modules • Name clash in code • Namespace used to resolve – Java uses package name, ++ uses namespace

• Ruby Modules serve as namespaces • But there’s more! • They provide Mixins

Venkat Subramaniam – [email protected] 2 Creating Module

Venkat Subramaniam – [email protected] 3

Mixins • Ruby provides single implementation inheritance – You may inherit from one class • What about Multiple inheritance? • Generally problematic in supported languages • Quite a few disallow this

• Ruby has an elegant approach – using Mixins Venkat Subramaniam – [email protected] 4 Mixins… • Instance methods of a module get mixed into a class that includes a module • Behaves as if your class inherited the module

Venkat Subramaniam – [email protected] 5

How does it mix in? • Ruby does not “copy” module methods to your class • All classes that include a module refer to the same module • Instance variables are not shared – they’re per instance – Use caution, variable names may collide • Mixing may assume your class has certain method and use it

Venkat Subramaniam – [email protected] 6 How does it mix in?...

Venkat Subramaniam – [email protected] 7

Method Search Order • When you call a method it searches for it in the following order: – Class of the object – Each of the Mixins (reverse order of include) – Super class – Each of its mixins – Super class’s mixins… –… – Finally class of the object’s method_missing method

Venkat Subramaniam – [email protected] 8 Search Order

Venkat Subramaniam – [email protected] 9

Duck Typing • Dynamic typing can come in handy • You don’t rely upon an explicit • You expect certain methods on the object and you utilize them • Any object that can fulfill that method call may be used • Objects type determined by what it can do, not based on its class • Rather than check for type, check for object’s capabilities • If you really have to ask, you can do that by calling respond_to? Method – obj.respond_to? (:method_name)

Venkat Subramaniam – [email protected] 10 Query for method

Venkat Subramaniam – [email protected] 11

Type Conversion • Sometimes you want to use a type as another • soft conversions –to_sand to_i • Strict conversion – happens by calls to –to_str –to_int –to_ary –to_hash –to_io –to_proc –to_sym

Venkat Subramaniam – [email protected] 12 Numerical Coercion • Classes may implement a coerce method that returns an array of same type of objects

•Helpful to work with different types

Venkat Subramaniam – [email protected] 13

Metadata • Each object has – of flags – Instance variables – References to its class object

• Class object has instance methods • Refers to its which has class methods • This meta class has a V flag for virtual class – Invisible class – Can’t be instantiated – Not listed as ancestor

Venkat Subramaniam – [email protected] 14 Methods specific to Objects • You can add methods per object

Venkat Subramaniam – [email protected] 15

Classes specific to Objects • Ruby allows you to introduce classes specific to objects – AOP has concept of introduction where you can change inheritance hierarchy • You are introducing a virtual class between the object and its class str1 String Virtual Class object Class String

String str2 object

Venkat Subramaniam – [email protected] 16 Extending • An object can extend a module

• obj.extend(Module_Name)

• above is equivalent to class <

Venkat Subramaniam – [email protected] 17

Extending a class • A class may include a Module – gets all instance methods of Module • A class may extend a Module – gets all module methods as class methods

Venkat Subramaniam – [email protected] 18