Confident Ruby
Be A Coding Hemingway
Confident Ruby
Avdi Grimm
http://www.rubytapas.com/
Doesn’t:

Does:

● Application Design

● Patterns for writing
confident Ruby
code at the method
level

● Object Modeling
“CODE
AS NARRATIVE”
Collecting Input
Performing Work
Delivering Output
Handling Errors
Responsibilities
Conversion Methods
Special Case Object
Receive Policies
Conversion Methods

Conversion Methods for Coercing Inputs
Type checking

Nested control structures
(‘if/else’ blocks)

Checking for ‘nil’ (type
checking)
Explicit Conversion
● #to_a, #to_i, #to_s
● objects don’t resemble the target type
● Implemented by Ruby types but not
cal...
Implicit Conversion
● #to_ary, #to_int, #to_str
● objects closely resemble the target type
● Called by Ruby but not implem...
Kernel Level Conversion Methods
● Array(), Integer(), String()
● Try implicit and explicit conversions
● Bag o’ tricks
Flexible
Coherent
Confident ; )
Conversion Methods

Special Case Object
Conversion Methods
=>
Special Case Object
● isolate the differences to a single object
● Leverage polymorphism by conforming to
a protocol
Confident
Expressive
Flexible

=>
“The foundation of an object
oriented system is the message”

-Sandi Metz, Practical Object-Oriented Design in Ruby
Conversion Methods

Receive Policies Instead of Data
Delegate Responsibility to Client Code
“Put the decision for how to handle
test cases in the hands of the code
best qualif...
● Intent of the method
is obscured
● Calling code is not
self-documenting
● Little flexibility.
What if we want
more than ...
● Pass policies as
block/proc/lambda
objects
● Stop handling edge
cases from obfuscating
the method’s intent
● Flexibility...
Example: Ruby’s
Enumerable#detect
Write code that
tells a story
Confident Ruby - Avdi Grimm
Upcoming SlideShare
Loading in...5
×

Confident Ruby: Be A Coding Hemingway

806

Published on

Book Report on Avdi Grimm's excellent 'Confident Ruby'

Published in: Technology, Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
806
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
5
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Confident Ruby: Be A Coding Hemingway

  1. 1. Confident Ruby Be A Coding Hemingway
  2. 2. Confident Ruby Avdi Grimm http://www.rubytapas.com/
  3. 3. Doesn’t: Does: ● Application Design ● Patterns for writing confident Ruby code at the method level ● Object Modeling
  4. 4. “CODE AS NARRATIVE”
  5. 5. Collecting Input Performing Work Delivering Output Handling Errors Responsibilities
  6. 6. Conversion Methods Special Case Object Receive Policies
  7. 7. Conversion Methods Conversion Methods for Coercing Inputs
  8. 8. Type checking Nested control structures (‘if/else’ blocks) Checking for ‘nil’ (type checking)
  9. 9. Explicit Conversion ● #to_a, #to_i, #to_s ● objects don’t resemble the target type ● Implemented by Ruby types but not called
  10. 10. Implicit Conversion ● #to_ary, #to_int, #to_str ● objects closely resemble the target type ● Called by Ruby but not implemented
  11. 11. Kernel Level Conversion Methods ● Array(), Integer(), String() ● Try implicit and explicit conversions ● Bag o’ tricks
  12. 12. Flexible Coherent Confident ; )
  13. 13. Conversion Methods Special Case Object
  14. 14. Conversion Methods =>
  15. 15. Special Case Object ● isolate the differences to a single object ● Leverage polymorphism by conforming to a protocol
  16. 16. Confident Expressive Flexible =>
  17. 17. “The foundation of an object oriented system is the message” -Sandi Metz, Practical Object-Oriented Design in Ruby
  18. 18. Conversion Methods Receive Policies Instead of Data
  19. 19. Delegate Responsibility to Client Code “Put the decision for how to handle test cases in the hands of the code best qualified to determine the appropriate response.”
  20. 20. ● 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? Passing data/flags
  21. 21. ● 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
  22. 22. Example: Ruby’s Enumerable#detect
  23. 23. Write code that tells a story Confident Ruby - Avdi Grimm
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×