Successfully reported this slideshow.

More Related Content

Related Books

Free with a 14 day trial from Scribd

See all

Related Audiobooks

Free with a 14 day trial from Scribd

See all

Ruby object model

  1. 1. The Ruby Object Model a presentation for the Nashville Software School 24 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-Oriented Design in Ruby Metaprogramming Ruby Paolo PerrottaSandi Metz
  4. 4. Credit Where Credit is Due... Eliza Brock elizabrocksoftware.com
  5. 5. Ruby and Objects In Ruby, everything is an object.* * except when it isn't
  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 -- String no love for foo :(
  13. 13. salutations salutations.foo def foo end
  14. 14. salutations salutations.foo def foo end
  15. 15. salutations salutations.foo def foo end String Object BasicObject def foo end def foo end def foo end
  16. 16. salutations salutations.foo def foo end String Object BasicObject def foo end def foo end def foo end NoMethodError
  17. 17. Impress Your Friends: Lookup what method_missing does.
  18. 18. salutations salutations.foo def foo end String Object BasicObject def foo end def foo end def foo end NoMethodError + method_missing
  19. 19. This Guy Says: "Doesn't this make Ruby soooo much slower than #{my favorite compiled language}?"
  20. 20. My Retort: Moving on...
  21. 21. salutations salutations.length def length end
  22. 22. salutations salutations.length def foo end String Object BasicObject def length end salutations.length == 13
  23. 23. Uncle Max's Story Time...
  24. 24. Inheritance or how I learned to stop worrying and love method lookup
  25. 25. A Simple Ruby Class: You
  26. 26. It's Alive!!!!!1
  27. 27. you == NSS Student me != NSS Student eliza != NSS Student
  28. 28. you == NSS Student me == junior dev eliza == accomplished professional
  29. 29. You Me Eliza We 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 requirements changed and "experience" were henceforth to be known as "awesome_points" ...? What about specialization? Why have three classes that do the same thing?
  33. 33. Inheritance Is All About Commonality ● Share common methods, attributes to keep it DRY ● Generalization vs. Specialization ● Abstract vs. Concrete use the object model/lookup
  34. 34. Specialization Same Method Name, Different Result Different Methods
  35. 35. Generalization NssStudent is a ...? JrDev is a ...? AccomplishedProfessional is a ...?
  36. 36. it fits!!!!!!!1 win
  37. 37. shared, generalized code extracted/abstracted specialized code call to super invokes the method in the super class overrides the superclass
  38. 38. The Abstract Class in Inheritance part of the lookup chain generally 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 emptor developer-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) Modules were all like:
  44. 44. Nowadays Modules are all like:
  45. 45. SoftWhere Co. App.
  46. 46. SoftWhere Co. App.
  47. 47. SoftWhere Co. App.
  48. 48. SoftWhere Co. App. Officeable's methods become instance methods for the Executive, MiddleManagement, and Developer models. win
  49. 49. SoftWhere Co. App. All three methods have a single, authoritative place where they live. That's D.R.Y. win
  50. 50. 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' ???
  51. 51. SoftWhere Co. App. win The next developer receives a helpful error message. Novel concept.
  52. 52. SoftWhere Co. App. Side note: you'll probably want to add a guard to make sure amount_of_unease is not zero.
  53. 53. A Rails Template Pattern Erich 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 viewed by anyone anywhere, in perpetuity, throughout the universe

×