Ruby object model

56
The Ruby Object Model a presentation for the Nashville Software School 24 June, 2013

Transcript of Ruby object model

Page 1: Ruby object model

The Ruby Object Modela presentation for the Nashville Software School

24 June, 2013

Page 2: Ruby object model

WHOAMI?

● Max Beizer => jr. developer @ Centresource

● NSS Cohort One graduate

● maxbeizer on:

○ twitter

○ github

○ irc

Page 3: Ruby object model

Credit Where Credit is Due...

Practical Object-Oriented Design in Ruby

Metaprogramming Ruby

Paolo PerrottaSandi Metz

Page 4: Ruby object model

Credit Where Credit is Due...

Eliza Brock

elizabrocksoftware.com

Page 5: Ruby object model

Ruby and Objects

In Ruby, everything is an object.*

* except when it isn't

Page 6: Ruby object model

see also: ancestors

Page 7: Ruby object model

Every Object "Returns to the Source"

BasicObject

Page 8: Ruby object model

Why do I care?

Page 9: Ruby object model

Why do I care?

Why does salutations respond to length and reverse, but not foo?

Page 10: Ruby object model

Remember:

Page 11: Ruby object model

ruby-doc.org -- String

Page 12: Ruby object model

ruby-doc.org -- String

no love for foo

:(

Page 13: Ruby object model

salutations

salutations.foo def fooend

Page 14: Ruby object model

salutations

salutations.foo def fooend

Page 15: Ruby object model

salutations

salutations.foo def fooend

String

Object

BasicObject

def fooend

def fooend

def fooend

Page 16: Ruby object model

salutations

salutations.foo def fooend

String

Object

BasicObject

def fooend

def fooend

def fooend

NoMethodError

Page 17: Ruby object model

Impress Your Friends:

Lookup what method_missing does.

Page 18: Ruby object model

salutations

salutations.foo def fooend

String

Object

BasicObject

def fooend

def fooend

def fooend

NoMethodError+

method_missing

Page 19: Ruby object model

This Guy Says:

"Doesn't this make Ruby soooo much slower than #{my favorite compiled language}?"

Page 20: Ruby object model

My Retort:

Moving on...

Page 21: Ruby object model

salutations

salutations.length def lengthend

Page 22: Ruby object model

salutations

salutations.length def fooend

String

Object

BasicObject

def lengthend

salutations.length == 13

Page 23: Ruby object model

Uncle Max's Story Time...

Page 24: Ruby object model

Inheritanceor how I learned to stop worrying and love method lookup

Page 25: Ruby object model

A Simple Ruby Class: You

Page 26: Ruby object model

It's Alive!!!!!1

Page 27: Ruby object model

you == NSS Student

me != NSS Student

eliza != NSS Student

Page 28: Ruby object model

you == NSS Student

me == junior dev

eliza == accomplished professional

Page 29: Ruby object model

You

Me

Eliza

We All Have:● name● experience● job(?)

Page 30: Ruby object model

Naïve Implementation(A.K.A. my middle names)

D.R.Y. ?

Page 31: Ruby object model

Naïve Implementation(A.K.A. my middle names)

D.R.Y.

Sopping wet

Page 32: Ruby object model

Naïve Implementation(A.K.A. my middle names)

What if the requirements changed and "experience" were henceforth to be known as "awesome_points" ...?

What about specialization? Why have three classes that do the same thing?

Page 33: Ruby object model

Inheritance Is All About Commonality

● Share common methods, attributes to keep it DRY● Generalization vs. Specialization● Abstract vs. Concrete

use the object model/lookup

Page 34: Ruby object model
Page 35: Ruby object model

Specialization

Same Method Name, Different Result

Different Methods

Page 36: Ruby object model

GeneralizationNssStudent is a ...?

JrDev is a ...?

AccomplishedProfessional is a ...?

Page 37: Ruby object model

it fits!!!!!!!1

win

Page 38: Ruby object model

shared, generalized codeextracted/abstracted

specialized code

call to superinvokes the method in the super class

overrides the superclass

Page 39: Ruby object model

The Abstract Class in Inheritance

part of the lookup chain

generally never to be instantiated on its own

Page 40: Ruby object model

Inheritance: is it right for you?Is it right for your problem set?

Ask yourself: is this an isa?

Page 41: Ruby object model

Inheritance: is it right for you?Is it right for your problem set?

caveat emptordeveloper-or

Page 42: Ruby object model

Inheritance: is it right for you?Is it right for your problem set?

put another way: not everything is a nail

Page 43: Ruby object model

Rails-Colored Glasses

Page 44: Ruby object model

Way back when I was first learning Ruby(pause for laughter)

Moduleswere all like:

Page 45: Ruby object model

Nowadays

Modulesare all like:

Page 46: Ruby object model

SoftWhere Co. App.

Page 47: Ruby object model

SoftWhere Co. App.

Page 48: Ruby object model

SoftWhere Co. App.

Page 49: Ruby object model

SoftWhere Co. App.

Officeable's methods become instance methods for the Executive, MiddleManagement, and Developer models.

win

Page 50: Ruby object model

SoftWhere Co. App.

All three methods have a single, authoritative place where they live. That's D.R.Y.

win

Page 51: Ruby object model

SoftWhere Co. App.

not so much

What if I create a new class that needs to include Officeable, but I forget about or don't know about 'amount_of_unease'???

Page 52: Ruby object model

SoftWhere Co. App.

win

The next developer receives a helpful error message. Novel concept.

Page 53: Ruby object model

SoftWhere Co. App.

Side note: you'll probably want to add a guard to make sure amount_of_unease is not zero.

Page 55: Ruby object model

Make sure lib is in the load path

Page 56: Ruby object model

image credits:● http://www.newgre.org/admissions/applying-doctoral-programs-it%E2%80%99s-match/attachment/square-peg-in-a-round-

hole/● amazon.com● elizabrocksoftware.com● http://www.hsxdude.com/● http://pragdave.pragprog.com/pragdave/2007/05/rails_is_love.html● http://creepypasta.wikia.com/wiki/File:Creepy-van.jpg● http://juliasetssail.blogspot.com/2010_04_01_archive.html● http://knowyourmeme.com/memes/haters-gonna-hate● http://blog.ausweb.com.au/system-administrator-appreciation-day/● http://worldtruth.tv/philosophy-the-matrix-return-to-the-source/

the preceding presentation is intended for educational purposes only and should not be viewed by anyone anywhere, in perpetuity, throughout the universe