objects, objects everywhere
TRANSCRIPT
Objects, Objects Everywhere
Mike Pack@zombidev
Let’s Talk Objects
Behavior+
State
Let’s Talk ObjectsBehavior
WalkTalkEat
Move LegsMove Lips
Ingest Food
I’m a person, what can I do?
Could also be defined as:
Let’s Talk ObjectsBehavior
Level of abstraction matters!Behavioral abstraction varies per domain.
Let’s Talk ObjectsState
Mike PackMale
Brown
NameGender
Hair Color
I’m a person, who am I currently?
Therefore, I have attributes:
Let’s Talk ObjectsState
States changeWhen I’m 70, my attributes might be:
Mike PackMaleGrey
Let’s Talk Objects
Behavior changes too!I just learned to jump, now my behavior is:
WalkTalkEat
Jump
Let’s Talk Objects
Behavior+
State
HTML
<div id=”name”> Mike</div>
Is this an object?
HTML
<div id=”name”> Mike</div>
Is this an object?
Attribute Name
HTML
<div id=”name”> Mike</div>
Is this an object?
State
Attribute Name
HTML
<div id=”name”> Mike</div>
Is this an object?
State
Attribute Name
Where’s the beef?
HTML
<div id=”name”> Mike</div>
Is this an object?
State
Attribute Name
Where’s the behavior?
HTML<div id=”name” onclick=”...”> Mike</div>
Is this an object?
HTML<div id=”name” onclick=”...”> Mike</div>
Is this an object?
Attribute Name
State
Behavior!
HTML
HTML is object oriented(albeit, not for purist)
HTML does not exhibit other properties of an OO systems:- Inheritance- Polymorphism- etc
HTML
Don’t use onclick=”...”However, without onclick, HTML does not exhibit behavior.
CSS
a { color: red;}
Is this an object?
a { color: red;}
CSS
Is this an object?
Attribute Name
a { color: red;}
CSS
Is this an object?
State
Attribute Name
a { color: red;}
CSS
Is this an object?
State
Attribute Name
Where’s the beef?
CSSa { color: red; &:hover { color: blue; }}
Is this an object?
a { color: red; &:hover { color: blue; }}
CSS
Is this an object?
Attribute Name
State
Behavior!
a { color: red; &:hover { color: blue; }}
CSS
State Change
Behavior
Behavior changes state.
CSS
CSS is object oriented(not for purist)
CSS does not exhibit other properties of an OO systems:- Inheritance- Polymorphism- etc
JavaScript
Is this an object?
var book = { title: “The Art of War”};
JavaScript
var book = { title: “The Art of War”};
Is this an object?
Attribute Name
JavaScript
var book = { title: “The Art of War”};
Is this an object?
Attribute Name
State
JavaScript
var book = { title: “The Art of War”};
Is this an object?
Attribute Name
StateWhere’s the beef?
JavaScript
var book = { title: “The Art of War”};
Is this an object?
Attribute Name
StateIt’s implicit!Where’s the beef?
JavaScript var book = { title: “The Art of War”, constructor: function() {...}, hasOwnProperty: function() {...}, ...};
Implicit behavior.
JavaScript
JavaScript is object oriented(of course)
JavaScript
It’s not entirely object orientedJavaScript has primitives, too.
JavaScript
If it’s an object, typeof knowstypeof {} === ‘object’ //=> true
JavaScript
Integers are not objectstypeof 1 === ‘object’ //=> false
JavaScript
undefined is not an objecttypeof undefined === ‘object’ //=> false
JavaScript
Literals are not always primitivestypeof [] === ‘object’ //=> true
JavaScript
typeof might not return objecttypeof true === ‘object’ //=> false
But booleans are objects!typeof true === ‘boolean’ //=> true
new Boolean(1).valueOf() === true //=> true
JavaScript
Careful, typeof can bitetypeof null === ‘object’ //=> true
new Null() //=> error
Ruby
1
Is this an object?
Ruby
11.real #=> 1
Is this an object?
State (accessor)
Ruby
11.real #=> 11 + 1 #=> 2Is this an object?
State (accessor)
Ruby 1
1.real #=> 11 + 1 #=> 21.+(1) #=> 2Is this an object?
Behavior
State (accessor)
Ruby
Ruby is object oriented
Ruby
Everything is an object
Ruby
Literal arrays are new objects[].object_id #=> 70318249770140[].object_id #=> 70318249594160
Ruby
Literal strings are new objects‘slurpy’.object_id #=> 70318249561400‘slurpy’.object_id #=> 70318249500340
Ruby
Literal hashes are new objects{}.object_id #=> 7019206373870
{}.object_id #=> 70192063701240
Ruby
Literal regexs are new objects//.object_id #=> 70192063385520//.object_id #=> 70192067965040
Ruby
Literal ranges are new objects(1..2).object_id #=> 70192067946460(1..2).object_id #=> 70192067921120
Ruby
Not everything creates a new object
Ruby has singleton objects (objects instantiated once).
Ruby
nil is a singletonnil.object_id #=> 4nil.object_id #=> 4
Ruby
booleans are singletonsfalse.object_id #=> 0false.object_id #=> 0true.object_id #=> 2true.object_id #=> 2
Ruby
Numbers are singletons1.object_id #=> 31.object_id #=> 3
Ruby
Numbers are singletons1.object_id #=> 31.object_id #=> 3
Fixnums
Ruby
Bignums are not singletons
4611686018427387904.object_id #=> 70192063730740
4611686018427387904.object_id #=> 70192063508580
Ruby
4611686018427387904.object_id #=> 70192063730740
4611686018427387904.object_id #=> 70192063508580
Why 4611686018427387904?
Bignums are not singletons
Ruby bytes = 0.size #=> 8
bytes to store integer
Ruby
bits = bytes * 8 #=> 64
8 bits per byte
64 bit machine
bytes = 0.size #=> 8
bytes to store integer
Ruby bytes = 0.size #=> 8
bytes to store integer
bits = bytes * 8 #=> 64
8 bits per byte
2 ** (bits - 2) #=> 4611686018427387904
1 bit for sign + 1 bit for Ruby
64 bit machine
Ruby
4611686018427387904Bignum
4611686018427387904 - 1Fixnum
Different objects.
Singleton object.
Ruby
Why does nil have object_id of 4?
1 have object_id of 3?
true have object_id of 2?
Ruby
Because Matz says sofalse.object_id #=> 0
0.object_id #=> 1true.object_id #=> 2
1.object_id #=> 3nil.object_id #=> 4
2.object_id #=> 5
Ruby
Find by object_idObjectSpace._id2ref(0) #=> falseObjectSpace._id2ref(1) #=> 0ObjectSpace._id2ref(2) #=> trueObjectSpace._id2ref(3) #=> 1ObjectSpace._id2ref(4) #=> nilObjectSpace._id2ref(5) #=> 2ObjectSpace._id2ref(6) #=> errorObjectSpace._id2ref(7) #=> 3ObjectSpace._id2ref(8) #=> error
Ruby
Negative object_ids-1.object_id #=> -1-2.object_id #=> -3-3.object_id #=> -5
Objects.useful? # => true
Objects.useful? # => trueConsistent modeling across the stack.
Objects.useful? # => trueHelpful in representing the real world.
Objects.useful? # => trueFun and expressive.
Thanks!@zombidev