confident ruby: be a coding hemingway
DESCRIPTION
Book Report on Avdi Grimm's excellent 'Confident Ruby'TRANSCRIPT
![Page 1: Confident Ruby: Be A Coding Hemingway](https://reader033.vdocuments.us/reader033/viewer/2022052507/558cfb2fd8b42a186f8b47a0/html5/thumbnails/1.jpg)
Confident RubyBe A Coding Hemingway
![Page 2: Confident Ruby: Be A Coding Hemingway](https://reader033.vdocuments.us/reader033/viewer/2022052507/558cfb2fd8b42a186f8b47a0/html5/thumbnails/2.jpg)
Confident Ruby
Avdi Grimmhttp://www.rubytapas.com/
![Page 3: Confident Ruby: Be A Coding Hemingway](https://reader033.vdocuments.us/reader033/viewer/2022052507/558cfb2fd8b42a186f8b47a0/html5/thumbnails/3.jpg)
● Application Design
● Object Modeling
● Patterns for writing confident Ruby code at the method level
Doesn’t: Does:
![Page 4: Confident Ruby: Be A Coding Hemingway](https://reader033.vdocuments.us/reader033/viewer/2022052507/558cfb2fd8b42a186f8b47a0/html5/thumbnails/4.jpg)
“CODE AS NARRATIVE”
![Page 5: Confident Ruby: Be A Coding Hemingway](https://reader033.vdocuments.us/reader033/viewer/2022052507/558cfb2fd8b42a186f8b47a0/html5/thumbnails/5.jpg)
Responsibilities
Collecting Input
Performing Work
Delivering Output
Handling Errors
![Page 6: Confident Ruby: Be A Coding Hemingway](https://reader033.vdocuments.us/reader033/viewer/2022052507/558cfb2fd8b42a186f8b47a0/html5/thumbnails/6.jpg)
Conversion Methods
Special Case Object
Receive Policies
![Page 7: Confident Ruby: Be A Coding Hemingway](https://reader033.vdocuments.us/reader033/viewer/2022052507/558cfb2fd8b42a186f8b47a0/html5/thumbnails/7.jpg)
Conversion Methods
Conversion Methods for Coercing Inputs
![Page 8: Confident Ruby: Be A Coding Hemingway](https://reader033.vdocuments.us/reader033/viewer/2022052507/558cfb2fd8b42a186f8b47a0/html5/thumbnails/8.jpg)
Checking for ‘nil’ (type checking)
Nested control structures(‘if/else’ blocks)
Type checking
![Page 9: Confident Ruby: Be A Coding Hemingway](https://reader033.vdocuments.us/reader033/viewer/2022052507/558cfb2fd8b42a186f8b47a0/html5/thumbnails/9.jpg)
● #to_a, #to_i, #to_s
● objects don’t resemble the target type
● Implemented by Ruby types but not called
Explicit Conversion
![Page 10: Confident Ruby: Be A Coding Hemingway](https://reader033.vdocuments.us/reader033/viewer/2022052507/558cfb2fd8b42a186f8b47a0/html5/thumbnails/10.jpg)
● #to_ary, #to_int, #to_str
● objects closely resemble the target type
● Called by Ruby but not implemented
Implicit Conversion
![Page 11: Confident Ruby: Be A Coding Hemingway](https://reader033.vdocuments.us/reader033/viewer/2022052507/558cfb2fd8b42a186f8b47a0/html5/thumbnails/11.jpg)
● Array(), Integer(), String()
● Try implicit and explicit conversions
● Bag o’ tricks
Kernel Level Conversion Methods
![Page 12: Confident Ruby: Be A Coding Hemingway](https://reader033.vdocuments.us/reader033/viewer/2022052507/558cfb2fd8b42a186f8b47a0/html5/thumbnails/12.jpg)
Flexible
Coherent
Confident ; )
![Page 13: Confident Ruby: Be A Coding Hemingway](https://reader033.vdocuments.us/reader033/viewer/2022052507/558cfb2fd8b42a186f8b47a0/html5/thumbnails/13.jpg)
Conversion Methods
Special Case Object
![Page 14: Confident Ruby: Be A Coding Hemingway](https://reader033.vdocuments.us/reader033/viewer/2022052507/558cfb2fd8b42a186f8b47a0/html5/thumbnails/14.jpg)
Conversion Methods
=>
![Page 15: Confident Ruby: Be A Coding Hemingway](https://reader033.vdocuments.us/reader033/viewer/2022052507/558cfb2fd8b42a186f8b47a0/html5/thumbnails/15.jpg)
Special Case Object● isolate the differences to a single object
● Leverage polymorphism by conforming to a protocol
![Page 16: Confident Ruby: Be A Coding Hemingway](https://reader033.vdocuments.us/reader033/viewer/2022052507/558cfb2fd8b42a186f8b47a0/html5/thumbnails/16.jpg)
=>
ConfidentExpressiveFlexible
![Page 17: Confident Ruby: Be A Coding Hemingway](https://reader033.vdocuments.us/reader033/viewer/2022052507/558cfb2fd8b42a186f8b47a0/html5/thumbnails/17.jpg)
“The foundation of an object oriented system is the message”
-Sandi Metz, Practical Object-Oriented Design in Ruby
![Page 18: Confident Ruby: Be A Coding Hemingway](https://reader033.vdocuments.us/reader033/viewer/2022052507/558cfb2fd8b42a186f8b47a0/html5/thumbnails/18.jpg)
Conversion Methods
Receive Policies Instead of Data
![Page 19: Confident Ruby: Be A Coding Hemingway](https://reader033.vdocuments.us/reader033/viewer/2022052507/558cfb2fd8b42a186f8b47a0/html5/thumbnails/19.jpg)
“Put the decision for how to handle test cases in the hands of the code best qualified to determine the appropriate response.”
Delegate Responsibility to Client Code
![Page 20: Confident Ruby: Be A Coding Hemingway](https://reader033.vdocuments.us/reader033/viewer/2022052507/558cfb2fd8b42a186f8b47a0/html5/thumbnails/20.jpg)
Passing data/flags
● Intent of the method is obscured
● Calling code is not self-documenting
● Little flexibility. What if we want more than 2 ways to handle errors?
![Page 21: Confident Ruby: Be A Coding Hemingway](https://reader033.vdocuments.us/reader033/viewer/2022052507/558cfb2fd8b42a186f8b47a0/html5/thumbnails/21.jpg)
● Pass policies as block/proc/lambda objects
● Stop handling edge cases from obfuscating the method’s intent
● Flexibility - let’s the client code handle decision making
Passing Policies
![Page 22: Confident Ruby: Be A Coding Hemingway](https://reader033.vdocuments.us/reader033/viewer/2022052507/558cfb2fd8b42a186f8b47a0/html5/thumbnails/22.jpg)
Example: Ruby’s Enumerable#detect
![Page 23: Confident Ruby: Be A Coding Hemingway](https://reader033.vdocuments.us/reader033/viewer/2022052507/558cfb2fd8b42a186f8b47a0/html5/thumbnails/23.jpg)
Write code that tells a storyConfident Ruby - Avdi Grimm