SlideShare ist ein Scribd-Unternehmen logo
1 von 70
Downloaden Sie, um offline zu lesen
Objects, Objects
 Everywhere
    Mike Pack
     @zombidev
Let’s Talk Objects

  Behavior
     +
   State
Let’s Talk Objects
         Behavior

I’m a person, what can I do?
            Walk
            Talk
            Eat

 Could also be deïŹned as:
          Move Legs
          Move Lips
         Ingest Food
Let’s Talk Objects
                 Behavior




Level of abstraction matters!
  Behavioral abstraction varies per domain.
Let’s Talk Objects
              State

I’m a person, who am I currently?
            Mike Pack
              Male
             Brown

  Therefore, I have attributes:
             Name
             Gender
            Hair Color
Let’s Talk Objects
              State



     States change
When I’m 70, my attributes might be:

            Mike Pack
              Male
              Grey
Let’s Talk Objects

 Behavior changes too!
I just learned to jump, now my behavior is:

                  Walk
                   Talk
                   Eat
                  Jump
Let’s Talk Objects

  Behavior
     +
   State
HTML

<div id=”name”>
  Mike
</div>
  Is this an object?
Attribute
       HTML         Name
<div id=”name”>
  Mike
</div>
  Is this an object?
Attribute
       HTML         Name
<div id=”name”>
  Mike
           State
</div>
  Is this an object?
Attribute
        HTML         Name
<div id=”name”>
  Mike the beef?
Where’s    State
</div>
   Is this an object?
Attribute
         HTML         Name
  <div id=”name”>
    Mike behavior?
Where’s the State
  </div>
    Is this an object?
HTML
<div id=”name”
   onclick=”...”>
  Mike
</div>
  Is this an object?
Attribute
       HTML         Name
<div id=”name”
   onclick=”...”>
  Mike      Behavior!
</div> State
  Is this an object?
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?
CSS
        Attribute
a{       Name

  color: red;
}
 Is this an object?
CSS
        Attribute
a{       Name

  color: red;
}             State

 Is this an object?
CSS
          Attribute
 a{        Name

   color:the beef?
Where’s   red;
 }             State

   Is this an object?
CSS
a{
  color: red;
  &:hover {
    color: blue;
  }
}
Is this an object?
Attribute     CSS
 Name a {
          color: red; Behavior!
          &:hover {
            color: blue;
          }
                           State
      }
      Is this an object?
CSS
       a{
Behavior color: red;     State
         &:hover {      Change
           color: blue;
         }
       }
   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
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
            JavaScript
    var book = {
     title: “The Art of
             War” State
    };
        Is this an object?
Attribute
 Name
            JavaScript
   var book = {
    title: “The Art of
   Where’s the beef?
            War” State
   };
        Is this an object?
Attribute
 Name
            JavaScript
   var book = {
    title: “The Art of
   Where’s the beef?
          It’sWar”
              implicit! State
   };
        Is this an object?
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 oriented
    JavaScript has primitives, too.
JavaScript


If it’s an object, typeof knows
   typeof {} === ‘object’ //=> true
JavaScript


Integers are not objects
typeof 1 === ‘object’ //=> false
JavaScript


  undeïŹned is not an object
typeof undeïŹned === ‘object’ //=> false
JavaScript


Literals are not always primitives
     typeof [] === ‘object’ //=> true
JavaScript

  typeof might not return object
     typeof true === ‘object’ //=> false
    typeof true === ‘boolean’ //=> true
         But booleans are objects!
new Boolean(1).valueOf() === true //=> true
JavaScript


Careful, typeof can bite
typeof null === ‘object’ //=> true
     new Null() //=> error
Ruby


       1

Is this an object?
Ruby        State
              (accessor)
     1
1.real #=> 1

Is this an object?
Ruby        State
              (accessor)
     1
1.real #=> 1
1 + 1 #=> 2
Is this an object?
Ruby        State
              1     (accessor)
       1.real #=> 1
Behavior 1 + 1 #=> 2
        1.+(1) #=> 2
      Is this an object?
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 singleton
 nil.object_id #=> 4
 nil.object_id #=> 4
Ruby

booleans are singletons
   false.object_id #=> 0
   false.object_id #=> 0
    true.object_id #=> 2
    true.object_id #=> 2
Ruby


Numbers are singletons
    1.object_id #=> 3
    1.object_id #=> 3
Ruby

     um s
  xn
Fi Numbers are singletons
        1.object_id #=> 3
        1.object_id #=> 3
Ruby

Bignums are not singletons
4611686018427387904.object_id #=>
        70192063730740
4611686018427387904.object_id #=>
        70192063508580
Ruby

  Bignums are not singletons
 4611686018427387904.object_id #=>
         70192063730740
 4611686018427387904.object_id #=>
         70192063508580


Why 4611686018427387904?
bytes to store
               Ruby       integer
bytes = 0.size #=> 8
bytes to store
                Ruby          integer
bytes = 0.size #=> 8
                          8 bits per byte

bits = bytes * 8 #=> 64
                          64 bit machine
bytes to store
                Ruby          integer
bytes = 0.size #=> 8
                          8 bits per byte

bits = bytes * 8 #=> 64
                          64 bit machine

                        1 bit for sign
                       + 1 bit for Ruby
2 ** (bits - 2) #=>
4611686018427387904
Ruby
 4611686018427387904
         Bignum
     DiïŹ€erent objects.


4611686018427387904 - 1
          Fixnum
      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 so
  false.object_id #=> 0
      0.object_id #=> 1
   true.object_id #=> 2
      1.object_id #=> 3
     nil.object_id #=> 4
      2.object_id #=> 5
Ruby
   Find by object_id
ObjectSpace._id2ref(0) #=> false
ObjectSpace._id2ref(1) #=> 0
ObjectSpace._id2ref(2) #=> true
ObjectSpace._id2ref(3) #=> 1
ObjectSpace._id2ref(4) #=> nil
ObjectSpace._id2ref(5) #=> 2
ObjectSpace._id2ref(6) #=> error
ObjectSpace._id2ref(7) #=> 3
ObjectSpace._id2ref(8) #=> error
Ruby

Negative object_ids
  -1.object_id #=> -1
  -2.object_id #=> -3
  -3.object_id #=> -5
Objects.useful? # => true
Objects.useful? # => true
Consistent modeling across the stack.
Objects.useful? # => true
Helpful in representing the real world.
Objects.useful? # => true
     Fun and expressive.
Thanks!
@zombidev

Weitere Àhnliche Inhalte

Was ist angesagt? (8)

Jazoon 2010 - Building DSLs with Eclipse
Jazoon 2010 - Building DSLs with EclipseJazoon 2010 - Building DSLs with Eclipse
Jazoon 2010 - Building DSLs with Eclipse
 
Json
JsonJson
Json
 
Introduction to JRuby
Introduction to JRubyIntroduction to JRuby
Introduction to JRuby
 
Introduction to XML
Introduction to XMLIntroduction to XML
Introduction to XML
 
Ruby objects
Ruby objectsRuby objects
Ruby objects
 
jQuery
jQueryjQuery
jQuery
 
jQuery
jQueryjQuery
jQuery
 
03 Object Relational Mapping
03 Object Relational Mapping03 Object Relational Mapping
03 Object Relational Mapping
 

Ähnlich wie Objects, Objects Everywhere

みゆっき☆Think#7 ă€ŒæœŹæ°—ă§ć­Šă¶Javascript」
みゆっき☆Think#7 ă€ŒæœŹæ°—ă§ć­Šă¶Javascript」みゆっき☆Think#7 ă€ŒæœŹæ°—ă§ć­Šă¶Javascript」
みゆっき☆Think#7 ă€ŒæœŹæ°—ă§ć­Šă¶Javascript」
techtalkdwango
 
珏7曞みゆっき☆Think æœŹæ°—ă§ć­Šă¶ JavaScript
珏7曞みゆっき☆Think æœŹæ°—ă§ć­Šă¶ JavaScript珏7曞みゆっき☆Think æœŹæ°—ă§ć­Šă¶ JavaScript
珏7曞みゆっき☆Think æœŹæ°—ă§ć­Šă¶ JavaScript
Takuya Fujimura
 
Understanding PHP objects
Understanding PHP objectsUnderstanding PHP objects
Understanding PHP objects
julien pauli
 
Symfony & Javascript. Combining the best of two worlds
Symfony & Javascript. Combining the best of two worldsSymfony & Javascript. Combining the best of two worlds
Symfony & Javascript. Combining the best of two worlds
Ignacio MartĂ­n
 
Evan Schultz - Angular Summit - 2016
Evan Schultz - Angular Summit - 2016Evan Schultz - Angular Summit - 2016
Evan Schultz - Angular Summit - 2016
Evan Schultz
 
Practical Ruby Projects (Alex Sharp)
Practical Ruby Projects (Alex Sharp)Practical Ruby Projects (Alex Sharp)
Practical Ruby Projects (Alex Sharp)
MongoSF
 

Ähnlich wie Objects, Objects Everywhere (20)

NinjaScript and Mizugumo 2011-02-05
NinjaScript and Mizugumo 2011-02-05NinjaScript and Mizugumo 2011-02-05
NinjaScript and Mizugumo 2011-02-05
 
みゆっき☆Think#7 ă€ŒæœŹæ°—ă§ć­Šă¶Javascript」
みゆっき☆Think#7 ă€ŒæœŹæ°—ă§ć­Šă¶Javascript」みゆっき☆Think#7 ă€ŒæœŹæ°—ă§ć­Šă¶Javascript」
みゆっき☆Think#7 ă€ŒæœŹæ°—ă§ć­Šă¶Javascript」
 
珏7曞みゆっき☆Think æœŹæ°—ă§ć­Šă¶ JavaScript
珏7曞みゆっき☆Think æœŹæ°—ă§ć­Šă¶ JavaScript珏7曞みゆっき☆Think æœŹæ°—ă§ć­Šă¶ JavaScript
珏7曞みゆっき☆Think æœŹæ°—ă§ć­Šă¶ JavaScript
 
Javascript Objects Deep Dive
Javascript Objects Deep DiveJavascript Objects Deep Dive
Javascript Objects Deep Dive
 
Art of Javascript
Art of JavascriptArt of Javascript
Art of Javascript
 
Things that every JavaScript developer should know by Rachel Appel at FrontCo...
Things that every JavaScript developer should know by Rachel Appel at FrontCo...Things that every JavaScript developer should know by Rachel Appel at FrontCo...
Things that every JavaScript developer should know by Rachel Appel at FrontCo...
 
Understanding PHP objects
Understanding PHP objectsUnderstanding PHP objects
Understanding PHP objects
 
Symfony & Javascript. Combining the best of two worlds
Symfony & Javascript. Combining the best of two worldsSymfony & Javascript. Combining the best of two worlds
Symfony & Javascript. Combining the best of two worlds
 
React JS and why it's awesome
React JS and why it's awesomeReact JS and why it's awesome
React JS and why it's awesome
 
Javascript
JavascriptJavascript
Javascript
 
"Javascript" por Tiago Rodrigues
"Javascript" por Tiago Rodrigues"Javascript" por Tiago Rodrigues
"Javascript" por Tiago Rodrigues
 
Ruby Classes
Ruby ClassesRuby Classes
Ruby Classes
 
Interesting Facts About Javascript
Interesting Facts About JavascriptInteresting Facts About Javascript
Interesting Facts About Javascript
 
Evan Schultz - Angular Summit - 2016
Evan Schultz - Angular Summit - 2016Evan Schultz - Angular Summit - 2016
Evan Schultz - Angular Summit - 2016
 
The Ruby Racer: under the hood
The Ruby Racer: under the hoodThe Ruby Racer: under the hood
The Ruby Racer: under the hood
 
WDB005.1 - JavaScript for Java Developers (Lecture 1)
WDB005.1 - JavaScript for Java Developers (Lecture 1)WDB005.1 - JavaScript for Java Developers (Lecture 1)
WDB005.1 - JavaScript for Java Developers (Lecture 1)
 
Practical Ruby Projects (Alex Sharp)
Practical Ruby Projects (Alex Sharp)Practical Ruby Projects (Alex Sharp)
Practical Ruby Projects (Alex Sharp)
 
Practical Ruby Projects with MongoDB - MongoSF
Practical Ruby Projects with MongoDB - MongoSFPractical Ruby Projects with MongoDB - MongoSF
Practical Ruby Projects with MongoDB - MongoSF
 
Reasons To Love Ruby
Reasons To Love RubyReasons To Love Ruby
Reasons To Love Ruby
 
Javascript
JavascriptJavascript
Javascript
 

KĂŒrzlich hochgeladen

+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎+971_581248768%)**%*]'#abortion pills for sale in dubai@
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 

KĂŒrzlich hochgeladen (20)

Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdf
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
A Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source MilvusA Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source Milvus
 

Objects, Objects Everywhere

  • 1. Objects, Objects Everywhere Mike Pack @zombidev
  • 2. Let’s Talk Objects Behavior + State
  • 3. Let’s Talk Objects Behavior I’m a person, what can I do? Walk Talk Eat Could also be deïŹned as: Move Legs Move Lips Ingest Food
  • 4. Let’s Talk Objects Behavior Level of abstraction matters! Behavioral abstraction varies per domain.
  • 5. Let’s Talk Objects State I’m a person, who am I currently? Mike Pack Male Brown Therefore, I have attributes: Name Gender Hair Color
  • 6. Let’s Talk Objects State States change When I’m 70, my attributes might be: Mike Pack Male Grey
  • 7. Let’s Talk Objects Behavior changes too! I just learned to jump, now my behavior is: Walk Talk Eat Jump
  • 8. Let’s Talk Objects Behavior + State
  • 9. HTML <div id=”name”> Mike </div> Is this an object?
  • 10. Attribute HTML Name <div id=”name”> Mike </div> Is this an object?
  • 11. Attribute HTML Name <div id=”name”> Mike State </div> Is this an object?
  • 12. Attribute HTML Name <div id=”name”> Mike the beef? Where’s State </div> Is this an object?
  • 13. Attribute HTML Name <div id=”name”> Mike behavior? Where’s the State </div> Is this an object?
  • 14. HTML <div id=”name” onclick=”...”> Mike </div> Is this an object?
  • 15. Attribute HTML Name <div id=”name” onclick=”...”> Mike Behavior! </div> State Is this an object?
  • 16. HTML HTML is object oriented (albeit, not for purist) HTML does not exhibit other properties of an OO systems: - Inheritance - Polymorphism - etc
  • 17. HTML Don’t use onclick=”...” However, without onclick, HTML does not exhibit behavior.
  • 18. CSS a{ color: red; } Is this an object?
  • 19. CSS Attribute a{ Name color: red; } Is this an object?
  • 20. CSS Attribute a{ Name color: red; } State Is this an object?
  • 21. CSS Attribute a{ Name color:the beef? Where’s red; } State Is this an object?
  • 22. CSS a{ color: red; &:hover { color: blue; } } Is this an object?
  • 23. Attribute CSS Name a { color: red; Behavior! &:hover { color: blue; } State } Is this an object?
  • 24. CSS a{ Behavior color: red; State &:hover { Change color: blue; } } Behavior changes state.
  • 25. CSS CSS is object oriented (not for purist) CSS does not exhibit other properties of an OO systems: - Inheritance - Polymorphism - etc
  • 26. JavaScript var book = { title: “The Art of War” }; Is this an object?
  • 27. Attribute Name JavaScript var book = { title: “The Art of War” }; Is this an object?
  • 28. Attribute Name JavaScript var book = { title: “The Art of War” State }; Is this an object?
  • 29. Attribute Name JavaScript var book = { title: “The Art of Where’s the beef? War” State }; Is this an object?
  • 30. Attribute Name JavaScript var book = { title: “The Art of Where’s the beef? It’sWar” implicit! State }; Is this an object?
  • 31. JavaScript var book = { title: “The Art of War”, constructor: function() {...}, hasOwnProperty: function() {...}, ... }; Implicit behavior.
  • 32. JavaScript JavaScript is object oriented (of course)
  • 33. JavaScript It’s not entirely object oriented JavaScript has primitives, too.
  • 34. JavaScript If it’s an object, typeof knows typeof {} === ‘object’ //=> true
  • 35. JavaScript Integers are not objects typeof 1 === ‘object’ //=> false
  • 36. JavaScript undeïŹned is not an object typeof undeïŹned === ‘object’ //=> false
  • 37. JavaScript Literals are not always primitives typeof [] === ‘object’ //=> true
  • 38. JavaScript typeof might not return object typeof true === ‘object’ //=> false typeof true === ‘boolean’ //=> true But booleans are objects! new Boolean(1).valueOf() === true //=> true
  • 39. JavaScript Careful, typeof can bite typeof null === ‘object’ //=> true new Null() //=> error
  • 40. Ruby 1 Is this an object?
  • 41. Ruby State (accessor) 1 1.real #=> 1 Is this an object?
  • 42. Ruby State (accessor) 1 1.real #=> 1 1 + 1 #=> 2 Is this an object?
  • 43. Ruby State 1 (accessor) 1.real #=> 1 Behavior 1 + 1 #=> 2 1.+(1) #=> 2 Is this an object?
  • 46. Ruby Literal arrays are new objects [].object_id #=> 70318249770140 [].object_id #=> 70318249594160
  • 47. Ruby Literal strings are new objects ‘slurpy’.object_id #=> 70318249561400 ‘slurpy’.object_id #=> 70318249500340
  • 48. Ruby Literal hashes are new objects {}.object_id #=> 7019206373870 {}.object_id #=> 70192063701240
  • 49. Ruby Literal regexs are new objects //.object_id #=> 70192063385520 //.object_id #=> 70192067965040
  • 50. Ruby Literal ranges are new objects (1..2).object_id #=> 70192067946460 (1..2).object_id #=> 70192067921120
  • 51. Ruby Not everything creates a new object Ruby has singleton objects (objects instantiated once).
  • 52. Ruby nil is a singleton nil.object_id #=> 4 nil.object_id #=> 4
  • 53. Ruby booleans are singletons false.object_id #=> 0 false.object_id #=> 0 true.object_id #=> 2 true.object_id #=> 2
  • 54. Ruby Numbers are singletons 1.object_id #=> 3 1.object_id #=> 3
  • 55. Ruby um s xn Fi Numbers are singletons 1.object_id #=> 3 1.object_id #=> 3
  • 56. Ruby Bignums are not singletons 4611686018427387904.object_id #=> 70192063730740 4611686018427387904.object_id #=> 70192063508580
  • 57. Ruby Bignums are not singletons 4611686018427387904.object_id #=> 70192063730740 4611686018427387904.object_id #=> 70192063508580 Why 4611686018427387904?
  • 58. bytes to store Ruby integer bytes = 0.size #=> 8
  • 59. bytes to store Ruby integer bytes = 0.size #=> 8 8 bits per byte bits = bytes * 8 #=> 64 64 bit machine
  • 60. bytes to store Ruby integer bytes = 0.size #=> 8 8 bits per byte bits = bytes * 8 #=> 64 64 bit machine 1 bit for sign + 1 bit for Ruby 2 ** (bits - 2) #=> 4611686018427387904
  • 61. Ruby 4611686018427387904 Bignum DiïŹ€erent objects. 4611686018427387904 - 1 Fixnum Singleton object.
  • 62. Ruby Why does nil have object_id of 4? 1 have object_id of 3? true have object_id of 2?
  • 63. Ruby Because Matz says so false.object_id #=> 0 0.object_id #=> 1 true.object_id #=> 2 1.object_id #=> 3 nil.object_id #=> 4 2.object_id #=> 5
  • 64. Ruby Find by object_id ObjectSpace._id2ref(0) #=> false ObjectSpace._id2ref(1) #=> 0 ObjectSpace._id2ref(2) #=> true ObjectSpace._id2ref(3) #=> 1 ObjectSpace._id2ref(4) #=> nil ObjectSpace._id2ref(5) #=> 2 ObjectSpace._id2ref(6) #=> error ObjectSpace._id2ref(7) #=> 3 ObjectSpace._id2ref(8) #=> error
  • 65. Ruby Negative object_ids -1.object_id #=> -1 -2.object_id #=> -3 -3.object_id #=> -5
  • 67. Objects.useful? # => true Consistent modeling across the stack.
  • 68. Objects.useful? # => true Helpful in representing the real world.
  • 69. Objects.useful? # => true Fun and expressive.