SlideShare a Scribd company logo
1 of 88
Advanced Ruby Idioms So Clean
You Can Eat Off Of Them
Brian Guthrie
bguthrie@thoughtworks.com
http://twitter.com/bguthrie
What this talk is about
“magic”
http://www.flickr.com/photos/cayusa/2962437091/
encouraging you to try things
preventing you from poking
yourself with the sharp end of a
         pointed stick
magic
magic
technical understanding
design maturity
What this talk is not about
trying to stop you
explaining why
Who this talk is for
Who this talk is for




0-6 months   6-12 months    12-36 months   36+ months
Who this talk is for




0-6 months     6-12 months    12-36 months   36+ months
     total
  noob sauce
Who this talk is for




0-6 months     6-12 months    12-36 months         36+ months
     total                                     hardened
  noob sauce                                 ruby veteran
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
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.
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)
ask questions anytime
           http://www.flickr.com/photos/jonnya/4212907371/
task:
build a custom validation framework
task:
build a custom validation framework
            a danger to yourself
                and others
Tip

Offload the heavy stuff to dedicated objects.
                 It’s worth the trouble.
Tip

Make sure your subclasses honor the behavior
            of their superclasses.
                 It’s worth the trouble.
a brief detour
   http://www.flickr.com/photos/garyseven/4434835473/
6
  (acceptable)
    types of
      Ruby
method signatures
0
1
2
{}
1{}
*
0 1 2 H 1H *
0 1 2 ... n
H 1H *
Tip

Stick to the Six Acceptable Ruby Method Signatures.
                      0, 1, 2, H, 1H, *
a danger to yourself
    and others
Tip

       Add behavior with
instance_eval or define_method,
      not method_missing.
http://www.flickr.com/photos/suneko/389852016/
                                                +
                                                http://www.flickr.com/photos/contusion/4066189755/
validations anywhere!
>> Object.new.method :valid?
=> #<Method: Object#valid?>
>> Object.new.method :valid?
=> #<Method: Object(BrianRecord::Extensions::Validations::InstanceMethods)#valid?>
Tip

Modularize and name any extensions you build.
           Some poor sucker, like me, has to maintain that thing.
Tip

Use self.included to leverage existing class behaviors.
                      Purely my own opinion.
a_very_bad_gem/lib/extensions/object/tap.rb
Tip

         Extend respectfully:
Check for competing implementations.
          Or I will hunt you down and slay you.
Tip

Write tests.
Write tests.
Write tests.
Write tests.
Write
tests.
Write
tests.
Write
tests.
Write
tests.
Write
tests.
Write
tests.
Write
tests.
YUM
RUBY WILL EAT YOU


 YUM
http://www.flickr.com/photos/pandiyan/146700348/
http://www.flickr.com/photos/bunchofpants/52816417/
Brian Guthrie
bguthrie@thoughtworks.com
http://twitter.com/bguthrie

questions?

More Related Content

Viewers also liked

Useful phrases for speaking
Useful phrases for speakingUseful phrases for speaking
Useful phrases for speaking
andydei
 
Sense relations & Semantics
Sense relations & SemanticsSense relations & Semantics
Sense relations & Semantics
Afuza Shara
 

Viewers also liked (14)

Idioms
IdiomsIdioms
Idioms
 
Ruby For Java Programmers
Ruby For Java ProgrammersRuby For Java Programmers
Ruby For Java Programmers
 
English Idioms
English IdiomsEnglish Idioms
English Idioms
 
Ruby on Rails for beginners
Ruby on Rails for beginnersRuby on Rails for beginners
Ruby on Rails for beginners
 
IELTS Training - Misused Phrases in the IELTS Writing/ Speaking
IELTS Training - Misused Phrases in the IELTS Writing/ SpeakingIELTS Training - Misused Phrases in the IELTS Writing/ Speaking
IELTS Training - Misused Phrases in the IELTS Writing/ Speaking
 
Phrase, clause, and sentence in syntax
Phrase, clause, and sentence in syntaxPhrase, clause, and sentence in syntax
Phrase, clause, and sentence in syntax
 
English idioms
English idiomsEnglish idioms
English idioms
 
How to Teach Yourself to Code
How to Teach Yourself to CodeHow to Teach Yourself to Code
How to Teach Yourself to Code
 
Speaking English (Linking Words)
Speaking English  (Linking Words)Speaking English  (Linking Words)
Speaking English (Linking Words)
 
Useful phrases for speaking
Useful phrases for speakingUseful phrases for speaking
Useful phrases for speaking
 
Ppt on World Of Smartphones
Ppt on World Of SmartphonesPpt on World Of Smartphones
Ppt on World Of Smartphones
 
Ruby for Java Developers
Ruby for Java DevelopersRuby for Java Developers
Ruby for Java Developers
 
The Noun Phrase - Power up your description - Writing skills
The Noun Phrase - Power up your description - Writing skillsThe Noun Phrase - Power up your description - Writing skills
The Noun Phrase - Power up your description - Writing skills
 
Sense relations & Semantics
Sense relations & SemanticsSense relations & Semantics
Sense relations & Semantics
 

Recently uploaded

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
 

Recently uploaded (20)

Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot ModelNavi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
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
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
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, ...
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
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
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
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
 
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
 

Advanced Ruby Idioms So Clean You Can Eat Off Of Them

Editor's Notes

  1. tragically, a repeat offender as a framework writer I&amp;#x2019;m that guy; I just go write frameworks
  2. Look, you&apos;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&apos;t stop you, and I&apos;d be disappointed if you didn&apos;t at least try, so in this talk I&apos;ll show you how to do it without poking your own eye out with the pointy end of a sharpened stick.
  3. Explaining why you&amp;#x2019;d want to do these things Discouraging you from trying
  4. Look, you&apos;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&apos;t stop you, and I&apos;d be disappointed if you didn&apos;t at least try, so in this talk I&apos;ll show you how to do it without poking your own eye out with the pointy end of a sharpened stick.
  5. introduction; this is our goal; goals are important with DSLs
  6. simple lambda, no use of options
  7. slow, have to implement respond_to, have to call super, fairly complicated
  8. a little bit faster, still not exactly clear what&amp;#x2019;s going on
  9. faster yet, no memory leaks, a little bit clearer what&amp;#x2019;s going on--not EXACTLY rails convention
  10. a bad use of open classes, but even if good, there&amp;#x2019;s a problem
  11. Ruby is the name of the crocodile.
  12. Smart devs - obligation to team to condense &amp; beautify &amp; DRY out most commonly-used idioms Refactor and metaprogram more to domain, less to infrastructure
  13. Never again.