The Ruby Object Modela presentation for the Nashville Software School24 June, 2013
WHOAMI?● Max Beizer => jr. developer @ Centresource● NSS Cohort One graduate● maxbeizer on:○ twitter○ github○ irc
Credit Where Credit is Due...Practical Object-OrientedDesign in RubyMetaprogramming RubyPaolo PerrottaSandi Metz
Credit Where Credit is Due...Eliza Brockelizabrocksoftware.com
Ruby and ObjectsIn Ruby, everything is an object.** except when it isnt
see also: ancestors
Every Object "Returns to the Source"BasicObject
Why do I care?
Why do I care?Why does salutations respond to length and reverse, but not foo?
Remember:
ruby-doc.org -- String
ruby-doc.org -- Stringno love for foo:(
salutationssalutations.foo def fooend
salutationssalutations.foo def fooend
salutationssalutations.foo def fooendStringObjectBasicObjectdef fooenddef fooenddef fooend
salutationssalutations.foo def fooendStringObjectBasicObjectdef fooenddef fooenddef fooendNoMethodError
Impress Your Friends:Lookup what method_missing does.
salutationssalutations.foo def fooendStringObjectBasicObjectdef fooenddef fooenddef fooendNoMethodError+method_missing
This Guy Says:"Doesnt this make Ruby soooo much slowerthan #{my favorite compiled language}?"
My Retort:Moving on...
salutationssalutations.length def lengthend
salutationssalutations.length def fooendStringObjectBasicObjectdef lengthendsalutations.length == 13
Uncle Maxs Story Time...
Inheritanceor how I learned to stop worrying and love method lookup
A Simple Ruby Class: You
Its Alive!!!!!1
you == NSS Studentme != NSS Studenteliza != NSS Student
you == NSS Studentme == junior develiza == accomplished professional
YouMeElizaWe All Have:● name● experience● job(?)
Naïve Implementation(A.K.A. my middle names)D.R.Y. ?
Naïve Implementation(A.K.A. my middle names)D.R.Y.Sopping wet
Naïve Implementation(A.K.A. my middle names)What if the requirementschanged and "experience"were henceforth to be knownas ...
Inheritance Is All About Commonality● Share common methods, attributes to keep it DRY● Generalization vs. Specialization● ...
SpecializationSame Method Name, Different ResultDifferent Methods
GeneralizationNssStudent is a ...?JrDev is a ...?AccomplishedProfessional is a ...?
it fits!!!!!!!1win
shared, generalized codeextracted/abstractedspecialized codecall to superinvokes the method in the super classoverrides th...
The Abstract Class in Inheritancepart of the lookup chaingenerally never to be instantiated on its own
Inheritance: is it right for you?Is it right for your problem set?Ask yourself: is this an isa?
Inheritance: is it right for you?Is it right for your problem set?caveat emptordeveloper-or
Inheritance: is it right for you?Is it right for your problem set?put another way: not everything is a nail
Rails-Colored Glasses
Way back when I was first learning Ruby(pause for laughter)Moduleswere all like:
NowadaysModulesare all like:
SoftWhere Co. App.
SoftWhere Co. App.
SoftWhere Co. App.
SoftWhere Co. App.Officeables methods become instancemethods for the Executive,MiddleManagement, and Developermodels.win
SoftWhere Co. App.All three methods have a single,authoritative place where they live.Thats D.R.Y.win
SoftWhere Co. App.not so muchWhat if I create anew class that needsto include Officeablebut I forget about ordont know abo...
SoftWhere Co. App.winThe next developerreceives a helpfulerror message.Novel concept.
SoftWhere Co. App.Side note: youll probably want to add a guard to make sureamount_of_unease is not zero.
A Rails Template PatternErich Gamma, Richard Helm, Ralph Johnson and John Vlissides
Make sure lib is in the load path
image credits:● http://www.newgre.org/admissions/applying-doctoral-programs-it%E2%80%99s-match/attachment/square-peg-in-a-...
Ruby object model
Upcoming SlideShare
Loading in …5
×

Ruby object model

383 views
291 views

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
383
On SlideShare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
4
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Ruby object model

  1. 1. The Ruby Object Modela presentation for the Nashville Software School24 June, 2013
  2. 2. WHOAMI?● Max Beizer => jr. developer @ Centresource● NSS Cohort One graduate● maxbeizer on:○ twitter○ github○ irc
  3. 3. Credit Where Credit is Due...Practical Object-OrientedDesign in RubyMetaprogramming RubyPaolo PerrottaSandi Metz
  4. 4. Credit Where Credit is Due...Eliza Brockelizabrocksoftware.com
  5. 5. Ruby and ObjectsIn Ruby, everything is an object.** except when it isnt
  6. 6. see also: ancestors
  7. 7. Every Object "Returns to the Source"BasicObject
  8. 8. Why do I care?
  9. 9. Why do I care?Why does salutations respond to length and reverse, but not foo?
  10. 10. Remember:
  11. 11. ruby-doc.org -- String
  12. 12. ruby-doc.org -- Stringno love for foo:(
  13. 13. salutationssalutations.foo def fooend
  14. 14. salutationssalutations.foo def fooend
  15. 15. salutationssalutations.foo def fooendStringObjectBasicObjectdef fooenddef fooenddef fooend
  16. 16. salutationssalutations.foo def fooendStringObjectBasicObjectdef fooenddef fooenddef fooendNoMethodError
  17. 17. Impress Your Friends:Lookup what method_missing does.
  18. 18. salutationssalutations.foo def fooendStringObjectBasicObjectdef fooenddef fooenddef fooendNoMethodError+method_missing
  19. 19. This Guy Says:"Doesnt this make Ruby soooo much slowerthan #{my favorite compiled language}?"
  20. 20. My Retort:Moving on...
  21. 21. salutationssalutations.length def lengthend
  22. 22. salutationssalutations.length def fooendStringObjectBasicObjectdef lengthendsalutations.length == 13
  23. 23. Uncle Maxs Story Time...
  24. 24. Inheritanceor how I learned to stop worrying and love method lookup
  25. 25. A Simple Ruby Class: You
  26. 26. Its Alive!!!!!1
  27. 27. you == NSS Studentme != NSS Studenteliza != NSS Student
  28. 28. you == NSS Studentme == junior develiza == accomplished professional
  29. 29. YouMeElizaWe All Have:● name● experience● job(?)
  30. 30. Naïve Implementation(A.K.A. my middle names)D.R.Y. ?
  31. 31. Naïve Implementation(A.K.A. my middle names)D.R.Y.Sopping wet
  32. 32. Naïve Implementation(A.K.A. my middle names)What if the requirementschanged and "experience"were henceforth to be knownas "awesome_points" ...?What about specialization?Why have three classes thatdo the same thing?
  33. 33. Inheritance Is All About Commonality● Share common methods, attributes to keep it DRY● Generalization vs. Specialization● Abstract vs. Concreteuse the object model/lookup
  34. 34. SpecializationSame Method Name, Different ResultDifferent Methods
  35. 35. GeneralizationNssStudent is a ...?JrDev is a ...?AccomplishedProfessional is a ...?
  36. 36. it fits!!!!!!!1win
  37. 37. shared, generalized codeextracted/abstractedspecialized codecall to superinvokes the method in the super classoverrides the superclass
  38. 38. The Abstract Class in Inheritancepart of the lookup chaingenerally never to be instantiated on its own
  39. 39. Inheritance: is it right for you?Is it right for your problem set?Ask yourself: is this an isa?
  40. 40. Inheritance: is it right for you?Is it right for your problem set?caveat emptordeveloper-or
  41. 41. Inheritance: is it right for you?Is it right for your problem set?put another way: not everything is a nail
  42. 42. Rails-Colored Glasses
  43. 43. Way back when I was first learning Ruby(pause for laughter)Moduleswere all like:
  44. 44. NowadaysModulesare all like:
  45. 45. SoftWhere Co. App.
  46. 46. SoftWhere Co. App.
  47. 47. SoftWhere Co. App.
  48. 48. SoftWhere Co. App.Officeables methods become instancemethods for the Executive,MiddleManagement, and Developermodels.win
  49. 49. SoftWhere Co. App.All three methods have a single,authoritative place where they live.Thats D.R.Y.win
  50. 50. SoftWhere Co. App.not so muchWhat if I create anew class that needsto include Officeablebut I forget about ordont know aboutamount_of_unease???
  51. 51. SoftWhere Co. App.winThe next developerreceives a helpfulerror message.Novel concept.
  52. 52. SoftWhere Co. App.Side note: youll probably want to add a guard to make sureamount_of_unease is not zero.
  53. 53. A Rails Template PatternErich Gamma, Richard Helm, Ralph Johnson and John Vlissides
  54. 54. Make sure lib is in the load path
  55. 55. 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 viewedby anyone anywhere, in perpetuity, throughout the universe

×