Advanced Ruby Idioms So Clean You Can Eat Off Of Them

12,811 views

Published on

Look, you're going to go write a bunch of weird, crazy, and quite possibly criminally insane code in Ruby at some point or another in your career. Maybe you already have. I can't stop you, and I'd be disappointed if you didn't at least try, so in this talk I'll show you how to do it without poking your own eye out with the pointy end of a sharpened stick.

Published in: Technology
1 Comment
19 Likes
Statistics
Notes
No Downloads
Views
Total views
12,811
On SlideShare
0
From Embeds
0
Number of Embeds
33
Actions
Shares
0
Downloads
203
Comments
1
Likes
19
Embeds 0
No embeds

No notes for slide

  • tragically, a repeat offender as a framework writer
    I’m that guy; I just go write frameworks
  • Look, you're going to go write a bunch of weird, crazy, and quite possibly criminally insane code in Ruby at some point or another in your career. Maybe you already have. I can't stop you, and I'd be disappointed if you didn't at least try, so in this talk I'll show you how to do it without poking your own eye out with the pointy end of a sharpened stick.






  • Explaining why you’d want to do these things
    Discouraging you from trying







  • Look, you're going to go write a bunch of weird, crazy, and quite possibly criminally insane code in Ruby at some point or another in your career. Maybe you already have. I can't stop you, and I'd be disappointed if you didn't at least try, so in this talk I'll show you how to do it without poking your own eye out with the pointy end of a sharpened stick.


  • introduction; this is our goal; goals are important with DSLs

  • simple lambda, no use of options































  • slow, have to implement respond_to, have to call super, fairly complicated
  • a little bit faster, still not exactly clear what’s going on
  • faster yet, no memory leaks, a little bit clearer what’s going on--not EXACTLY rails convention



  • a bad use of open classes, but even if good, there’s a problem



















  • Ruby is the name of the crocodile.
  • Smart devs - obligation to team to condense & beautify & DRY out most commonly-used idioms
    Refactor and metaprogram more to domain, less to infrastructure
  • Never again.

  • Advanced Ruby Idioms So Clean You Can Eat Off Of Them

    1. Advanced Ruby Idioms So Clean You Can Eat Off Of Them Brian Guthrie bguthrie@thoughtworks.com http://twitter.com/bguthrie
    2. What this talk is about
    3. “magic” http://www.flickr.com/photos/cayusa/2962437091/
    4. encouraging you to try things
    5. preventing you from poking yourself with the sharp end of a pointed stick
    6. magic
    7. magic
    8. technical understanding
    9. design maturity
    10. What this talk is not about
    11. trying to stop you
    12. explaining why
    13. Who this talk is for
    14. Who this talk is for 0-6 months 6-12 months 12-36 months 36+ months
    15. Who this talk is for 0-6 months 6-12 months 12-36 months 36+ months total noob sauce
    16. Who this talk is for 0-6 months 6-12 months 12-36 months 36+ months total hardened noob sauce ruby veteran
    17. Who this talk is for 0-6 months 6-12 months 12-36 months 36+ months total a danger to yourself hardened noob sauce and others ruby veteran
    18. Who this talk is for 0-6 months 6-12 months 12-36 months 36+ months total a danger to yourself hardened noob sauce and others ruby veteran You know who you are.
    19. Who this talk is for 0-6 months 6-12 months 12-36 months 36+ months total thick controllers total lack of curiousity noob sauce (total noob sauce)
    20. ask questions anytime http://www.flickr.com/photos/jonnya/4212907371/
    21. task: build a custom validation framework
    22. task: build a custom validation framework a danger to yourself and others
    23. Tip Offload the heavy stuff to dedicated objects. It’s worth the trouble.
    24. Tip Make sure your subclasses honor the behavior of their superclasses. It’s worth the trouble.
    25. a brief detour http://www.flickr.com/photos/garyseven/4434835473/
    26. 6 (acceptable) types of Ruby method signatures
    27. 0
    28. 1
    29. 2
    30. {}
    31. 1{}
    32. *
    33. 0 1 2 H 1H *
    34. 0 1 2 ... n
    35. H 1H *
    36. Tip Stick to the Six Acceptable Ruby Method Signatures. 0, 1, 2, H, 1H, *
    37. a danger to yourself and others
    38. Tip Add behavior with instance_eval or define_method, not method_missing.
    39. http://www.flickr.com/photos/suneko/389852016/ + http://www.flickr.com/photos/contusion/4066189755/
    40. validations anywhere!
    41. >> Object.new.method :valid? => #<Method: Object#valid?>
    42. >> Object.new.method :valid? => #<Method: Object(BrianRecord::Extensions::Validations::InstanceMethods)#valid?>
    43. Tip Modularize and name any extensions you build. Some poor sucker, like me, has to maintain that thing.
    44. Tip Use self.included to leverage existing class behaviors. Purely my own opinion.
    45. a_very_bad_gem/lib/extensions/object/tap.rb
    46. Tip Extend respectfully: Check for competing implementations. Or I will hunt you down and slay you.
    47. Tip Write tests.
    48. Write tests.
    49. Write tests.
    50. Write tests.
    51. Write tests.
    52. Write tests.
    53. Write tests.
    54. Write tests.
    55. Write tests.
    56. Write tests.
    57. Write tests.
    58. YUM RUBY WILL EAT YOU YUM http://www.flickr.com/photos/pandiyan/146700348/
    59. http://www.flickr.com/photos/bunchofpants/52816417/
    60. Brian Guthrie bguthrie@thoughtworks.com http://twitter.com/bguthrie questions?

    ×