SlideShare ist ein Scribd-Unternehmen logo
1 von 88
Downloaden Sie, um offline zu lesen
Advanced Ruby Idioms So Clean
You Can Eat Off Of Them
Brian Guthrie
bguthrie@thoughtworks.com
http://twitter.com/bguthrie
Advanced Ruby Idioms So Clean You Can Eat Off Of Them
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
Advanced Ruby Idioms So Clean You Can Eat Off Of Them
Advanced Ruby Idioms So Clean You Can Eat Off Of Them
Advanced Ruby Idioms So Clean You Can Eat Off Of Them
Advanced Ruby Idioms So Clean You Can Eat Off Of Them
Advanced Ruby Idioms So Clean You Can Eat Off Of Them
Advanced Ruby Idioms So Clean You Can Eat Off Of Them
Advanced Ruby Idioms So Clean You Can Eat Off Of Them
Advanced Ruby Idioms So Clean You Can Eat Off Of Them
Tip

Offload the heavy stuff to dedicated objects.
                 It’s worth the trouble.
Advanced Ruby Idioms So Clean You Can Eat Off Of Them
Advanced Ruby Idioms So Clean You Can Eat Off Of Them
Advanced Ruby Idioms So Clean You Can Eat Off Of Them
Advanced Ruby Idioms So Clean You Can Eat Off Of Them
Advanced Ruby Idioms So Clean You Can Eat Off Of Them
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
Advanced Ruby Idioms So Clean You Can Eat Off Of Them
H 1H *
Advanced Ruby Idioms So Clean You Can Eat Off Of Them
Advanced Ruby Idioms So Clean You Can Eat Off Of Them
Advanced Ruby Idioms So Clean You Can Eat Off Of Them
Tip

Stick to the Six Acceptable Ruby Method Signatures.
                      0, 1, 2, H, 1H, *
Advanced Ruby Idioms So Clean You Can Eat Off Of Them
Advanced Ruby Idioms So Clean You Can Eat Off Of Them
Advanced Ruby Idioms So Clean You Can Eat Off Of Them
a danger to yourself
    and others
Advanced Ruby Idioms So Clean You Can Eat Off Of Them
Advanced Ruby Idioms So Clean You Can Eat Off Of Them
Advanced Ruby Idioms So Clean You Can Eat Off Of Them
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!
Advanced Ruby Idioms So Clean You Can Eat Off Of Them
>> Object.new.method :valid?
=> #<Method: Object#valid?>
Advanced Ruby Idioms So Clean You Can Eat Off Of Them
>> 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
Advanced Ruby Idioms So Clean You Can Eat Off Of Them
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/
Advanced Ruby Idioms So Clean You Can Eat Off Of Them
Brian Guthrie
bguthrie@thoughtworks.com
http://twitter.com/bguthrie

questions?

Weitere ähnliche Inhalte

Andere mochten auch

Ruby For Java Programmers
Ruby For Java ProgrammersRuby For Java Programmers
Ruby For Java ProgrammersMike Bowler
 
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/ SpeakingJROOZ Review Center
 
Phrase, clause, and sentence in syntax
Phrase, clause, and sentence in syntaxPhrase, clause, and sentence in syntax
Phrase, clause, and sentence in syntaxMuhamad Umar Chatab
 
English idioms
English idiomsEnglish idioms
English idiomssnowmaid
 
How to Teach Yourself to Code
How to Teach Yourself to CodeHow to Teach Yourself to Code
How to Teach Yourself to CodeMattan Griffel
 
Speaking English (Linking Words)
Speaking English  (Linking Words)Speaking English  (Linking Words)
Speaking English (Linking Words)sandeep J
 
Useful phrases for speaking
Useful phrases for speakingUseful phrases for speaking
Useful phrases for speakingandydei
 
Ppt on World Of Smartphones
Ppt on World Of SmartphonesPpt on World Of Smartphones
Ppt on World Of SmartphonesPulkit Syal
 
Ruby for Java Developers
Ruby for Java DevelopersRuby for Java Developers
Ruby for Java DevelopersRobert Reiz
 
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 skillsKinga Brady
 
Sense relations & Semantics
Sense relations & SemanticsSense relations & Semantics
Sense relations & SemanticsAfuza Shara
 

Andere mochten auch (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
 

Kürzlich hochgeladen

Linked Data in Production: Moving Beyond Ontologies
Linked Data in Production: Moving Beyond OntologiesLinked Data in Production: Moving Beyond Ontologies
Linked Data in Production: Moving Beyond OntologiesDavid Newbury
 
Designing A Time bound resource download URL
Designing A Time bound resource download URLDesigning A Time bound resource download URL
Designing A Time bound resource download URLRuncy Oommen
 
Cybersecurity Workshop #1.pptx
Cybersecurity Workshop #1.pptxCybersecurity Workshop #1.pptx
Cybersecurity Workshop #1.pptxGDSC PJATK
 
IaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdf
IaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdfIaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdf
IaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdfDaniel Santiago Silva Capera
 
COMPUTER 10: Lesson 7 - File Storage and Online Collaboration
COMPUTER 10: Lesson 7 - File Storage and Online CollaborationCOMPUTER 10: Lesson 7 - File Storage and Online Collaboration
COMPUTER 10: Lesson 7 - File Storage and Online Collaborationbruanjhuli
 
Apres-Cyber - The Data Dilemma: Bridging Offensive Operations and Machine Lea...
Apres-Cyber - The Data Dilemma: Bridging Offensive Operations and Machine Lea...Apres-Cyber - The Data Dilemma: Bridging Offensive Operations and Machine Lea...
Apres-Cyber - The Data Dilemma: Bridging Offensive Operations and Machine Lea...Will Schroeder
 
Bird eye's view on Camunda open source ecosystem
Bird eye's view on Camunda open source ecosystemBird eye's view on Camunda open source ecosystem
Bird eye's view on Camunda open source ecosystemAsko Soukka
 
NIST Cybersecurity Framework (CSF) 2.0 Workshop
NIST Cybersecurity Framework (CSF) 2.0 WorkshopNIST Cybersecurity Framework (CSF) 2.0 Workshop
NIST Cybersecurity Framework (CSF) 2.0 WorkshopBachir Benyammi
 
20230202 - Introduction to tis-py
20230202 - Introduction to tis-py20230202 - Introduction to tis-py
20230202 - Introduction to tis-pyJamie (Taka) Wang
 
Anypoint Code Builder , Google Pub sub connector and MuleSoft RPA
Anypoint Code Builder , Google Pub sub connector and MuleSoft RPAAnypoint Code Builder , Google Pub sub connector and MuleSoft RPA
Anypoint Code Builder , Google Pub sub connector and MuleSoft RPAshyamraj55
 
Machine Learning Model Validation (Aijun Zhang 2024).pdf
Machine Learning Model Validation (Aijun Zhang 2024).pdfMachine Learning Model Validation (Aijun Zhang 2024).pdf
Machine Learning Model Validation (Aijun Zhang 2024).pdfAijun Zhang
 
Connector Corner: Extending LLM automation use cases with UiPath GenAI connec...
Connector Corner: Extending LLM automation use cases with UiPath GenAI connec...Connector Corner: Extending LLM automation use cases with UiPath GenAI connec...
Connector Corner: Extending LLM automation use cases with UiPath GenAI connec...DianaGray10
 
VoIP Service and Marketing using Odoo and Asterisk PBX
VoIP Service and Marketing using Odoo and Asterisk PBXVoIP Service and Marketing using Odoo and Asterisk PBX
VoIP Service and Marketing using Odoo and Asterisk PBXTarek Kalaji
 
Artificial Intelligence & SEO Trends for 2024
Artificial Intelligence & SEO Trends for 2024Artificial Intelligence & SEO Trends for 2024
Artificial Intelligence & SEO Trends for 2024D Cloud Solutions
 
AI You Can Trust - Ensuring Success with Data Integrity Webinar
AI You Can Trust - Ensuring Success with Data Integrity WebinarAI You Can Trust - Ensuring Success with Data Integrity Webinar
AI You Can Trust - Ensuring Success with Data Integrity WebinarPrecisely
 
UiPath Studio Web workshop series - Day 6
UiPath Studio Web workshop series - Day 6UiPath Studio Web workshop series - Day 6
UiPath Studio Web workshop series - Day 6DianaGray10
 
Basic Building Blocks of Internet of Things.
Basic Building Blocks of Internet of Things.Basic Building Blocks of Internet of Things.
Basic Building Blocks of Internet of Things.YounusS2
 
IESVE Software for Florida Code Compliance Using ASHRAE 90.1-2019
IESVE Software for Florida Code Compliance Using ASHRAE 90.1-2019IESVE Software for Florida Code Compliance Using ASHRAE 90.1-2019
IESVE Software for Florida Code Compliance Using ASHRAE 90.1-2019IES VE
 
Nanopower In Semiconductor Industry.pdf
Nanopower  In Semiconductor Industry.pdfNanopower  In Semiconductor Industry.pdf
Nanopower In Semiconductor Industry.pdfPedro Manuel
 

Kürzlich hochgeladen (20)

Linked Data in Production: Moving Beyond Ontologies
Linked Data in Production: Moving Beyond OntologiesLinked Data in Production: Moving Beyond Ontologies
Linked Data in Production: Moving Beyond Ontologies
 
Designing A Time bound resource download URL
Designing A Time bound resource download URLDesigning A Time bound resource download URL
Designing A Time bound resource download URL
 
201610817 - edge part1
201610817 - edge part1201610817 - edge part1
201610817 - edge part1
 
Cybersecurity Workshop #1.pptx
Cybersecurity Workshop #1.pptxCybersecurity Workshop #1.pptx
Cybersecurity Workshop #1.pptx
 
IaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdf
IaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdfIaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdf
IaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdf
 
COMPUTER 10: Lesson 7 - File Storage and Online Collaboration
COMPUTER 10: Lesson 7 - File Storage and Online CollaborationCOMPUTER 10: Lesson 7 - File Storage and Online Collaboration
COMPUTER 10: Lesson 7 - File Storage and Online Collaboration
 
Apres-Cyber - The Data Dilemma: Bridging Offensive Operations and Machine Lea...
Apres-Cyber - The Data Dilemma: Bridging Offensive Operations and Machine Lea...Apres-Cyber - The Data Dilemma: Bridging Offensive Operations and Machine Lea...
Apres-Cyber - The Data Dilemma: Bridging Offensive Operations and Machine Lea...
 
Bird eye's view on Camunda open source ecosystem
Bird eye's view on Camunda open source ecosystemBird eye's view on Camunda open source ecosystem
Bird eye's view on Camunda open source ecosystem
 
NIST Cybersecurity Framework (CSF) 2.0 Workshop
NIST Cybersecurity Framework (CSF) 2.0 WorkshopNIST Cybersecurity Framework (CSF) 2.0 Workshop
NIST Cybersecurity Framework (CSF) 2.0 Workshop
 
20230202 - Introduction to tis-py
20230202 - Introduction to tis-py20230202 - Introduction to tis-py
20230202 - Introduction to tis-py
 
Anypoint Code Builder , Google Pub sub connector and MuleSoft RPA
Anypoint Code Builder , Google Pub sub connector and MuleSoft RPAAnypoint Code Builder , Google Pub sub connector and MuleSoft RPA
Anypoint Code Builder , Google Pub sub connector and MuleSoft RPA
 
Machine Learning Model Validation (Aijun Zhang 2024).pdf
Machine Learning Model Validation (Aijun Zhang 2024).pdfMachine Learning Model Validation (Aijun Zhang 2024).pdf
Machine Learning Model Validation (Aijun Zhang 2024).pdf
 
Connector Corner: Extending LLM automation use cases with UiPath GenAI connec...
Connector Corner: Extending LLM automation use cases with UiPath GenAI connec...Connector Corner: Extending LLM automation use cases with UiPath GenAI connec...
Connector Corner: Extending LLM automation use cases with UiPath GenAI connec...
 
VoIP Service and Marketing using Odoo and Asterisk PBX
VoIP Service and Marketing using Odoo and Asterisk PBXVoIP Service and Marketing using Odoo and Asterisk PBX
VoIP Service and Marketing using Odoo and Asterisk PBX
 
Artificial Intelligence & SEO Trends for 2024
Artificial Intelligence & SEO Trends for 2024Artificial Intelligence & SEO Trends for 2024
Artificial Intelligence & SEO Trends for 2024
 
AI You Can Trust - Ensuring Success with Data Integrity Webinar
AI You Can Trust - Ensuring Success with Data Integrity WebinarAI You Can Trust - Ensuring Success with Data Integrity Webinar
AI You Can Trust - Ensuring Success with Data Integrity Webinar
 
UiPath Studio Web workshop series - Day 6
UiPath Studio Web workshop series - Day 6UiPath Studio Web workshop series - Day 6
UiPath Studio Web workshop series - Day 6
 
Basic Building Blocks of Internet of Things.
Basic Building Blocks of Internet of Things.Basic Building Blocks of Internet of Things.
Basic Building Blocks of Internet of Things.
 
IESVE Software for Florida Code Compliance Using ASHRAE 90.1-2019
IESVE Software for Florida Code Compliance Using ASHRAE 90.1-2019IESVE Software for Florida Code Compliance Using ASHRAE 90.1-2019
IESVE Software for Florida Code Compliance Using ASHRAE 90.1-2019
 
Nanopower In Semiconductor Industry.pdf
Nanopower  In Semiconductor Industry.pdfNanopower  In Semiconductor Industry.pdf
Nanopower In Semiconductor Industry.pdf
 

Advanced Ruby Idioms So Clean You Can Eat Off Of Them

Hinweis der Redaktion

  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.