Code to Joy Introduction My Job is Awesome • My job is awesome • Remotely pairing with many different people Avdi Grimm (ShipRise) GoGaRuCo 2012 3 / 50
Code to Joy Introduction My Job is Awesome • My job is awesome • Remotely pairing with many different people • Different levels of knowledge, different problems to solve Avdi Grimm (ShipRise) GoGaRuCo 2012 3 / 50
Code to Joy Introduction My Job is Awesome • My job is awesome • Remotely pairing with many different people • Different levels of knowledge, different problems to solve • ”That’s so cool, I didn’t know you could do that” Avdi Grimm (ShipRise) GoGaRuCo 2012 3 / 50
Code to Joy Introduction Ruby is Awesome Ruby is designed to make programmers happy. — Matz It still finds ways to make me smile after all these years. Avdi Grimm (ShipRise) GoGaRuCo 2012 4 / 50
Code to Joy Introduction Postcards are Awesome • Work in progress: Confident Ruby • ”Pay with a postcard” • Postcards make me happy! Avdi Grimm (ShipRise) GoGaRuCo 2012 5 / 50
Code to Joy Introduction The Plan • A random walk through the Ruby language and standard library. . . • . . . stopping in at some of my favorite idioms and tools. . . Avdi Grimm (ShipRise) GoGaRuCo 2012 6 / 50
Code to Joy Introduction The Plan • A random walk through the Ruby language and standard library. . . • . . . stopping in at some of my favorite idioms and tools. . . • . . . and around the world, in postcards! Avdi Grimm (ShipRise) GoGaRuCo 2012 6 / 50
Code to Joy Splats Another client wants an object response = send_request case response.code when 400..599 then handle_error(response) else # ... end Avdi Grimm (ShipRise) GoGaRuCo 2012 12 / 50
Code to Joy Splats Using the response Struct as-is response = send_request case response.code when 400..599 then handle_error(response) else # ... end Avdi Grimm (ShipRise) GoGaRuCo 2012 18 / 50
Code to Joy Splats Splats & Structs • Return multiple values from a method. . . • . . . *Or* a single result object. . . • . . . from the same line of code. Magic! Avdi Grimm (ShipRise) GoGaRuCo 2012 19 / 50
Code to Joy YAML::Store The Persisted Data cat blog.yml --- :posts: - !ruby/struct:Post title: F1rst Post!!! body: FIRST - !ruby/struct:Post title: Hiatus body: This blog is on vacation Avdi Grimm (ShipRise) GoGaRuCo 2012 22 / 50
Code to Joy YAML::Store PStore • Drop-in replacement require ’pstore’ 2 repo = PStore.new( ’blog.pstore’ ) repo.transaction do 4 # ... end • Faster Adding 1000 posts, with a transaction for each write: user system total real yaml 50.210000 0.210000 50.420000 ( 54.024277) pstore 3.290000 0.130000 3.420000 ( 10.887887) Avdi Grimm (ShipRise) GoGaRuCo 2012 25 / 50
Code to Joy YAML::Store YAML#Store • Incredibly simple Hash-like persistence mechanism • Great for local command-line apps • Supports complex object trees • Oh yeah, one more thing. . . Avdi Grimm (ShipRise) GoGaRuCo 2012 26 / 50
Code to Joy Enumerators Enumerator and #to_enum • Turn any method into an iterable series • Make all the power of Enumerator available to the series Avdi Grimm (ShipRise) GoGaRuCo 2012 32 / 50
Code to Joy Break break It breaks out of loops. Whoop-de-doo. jobs.each do |job| break if job == :terminate job.call end Avdi Grimm (ShipRise) GoGaRuCo 2012 34 / 50
Code to Joy Break break beyond iteration i = 0 names do |name| puts name break if i >= 1 # that’s plenty! i += 1 end Ylva Brighid Avdi Grimm (ShipRise) GoGaRuCo 2012 35 / 50
Code to Joy Break break respects ensure blocks i = 0 names_with_last do |name| puts name break if i >= 1 # that’s plenty! i += 1 end Ylva Brighid Grimm Avdi Grimm (ShipRise) GoGaRuCo 2012 36 / 50
Code to Joy Break break with a value What if we want to capture a name? result = nil result = names do |name| if name =~ /^S/ result = name break end end result # => "Shifra" Avdi Grimm (ShipRise) GoGaRuCo 2012 37 / 50
Code to Joy Break break with a value result = names do |name| break name if name =~ /^S/ end result # => "Shifra" This effectively overrides the method’s return value. Avdi Grimm (ShipRise) GoGaRuCo 2012 37 / 50
Code to Joy Break Breaking from a #detect f = open( ’code-to-joy.org’ ) result = f.lines.detect do |line| break "" if f.lineno >= 100 line =~ /banana/ end result # => "" Avdi Grimm (ShipRise) GoGaRuCo 2012 38 / 50
Code to Joy Subclassing Module What we’d like Ideally, attributes would be delegated to either race or char_class. player.name # => "Rhincewind" player.strength # => 9 player.charisma # => 3 Avdi Grimm (ShipRise) GoGaRuCo 2012 41 / 50
Code to Joy Subclassing Module Using SimpleDelegator require ’delegate’ class Character < SimpleDelegator def initialize(name, race, char_class) @name = name super(race) end # ... end Only one object supported. Avdi Grimm (ShipRise) GoGaRuCo 2012 44 / 50
Code to Joy Subclassing Module Subclassing Module class Delegate < Module def initialize(attribute) @attribute = attribute super() do # see next slide... end end end Avdi Grimm (ShipRise) GoGaRuCo 2012 45 / 50
Code to Joy Subclassing Module Subclassing Module Character = Struct.new( :name , :race , :char_class ) do include Delegate.new( :race ) include Delegate.new( :char_class ) end Avdi Grimm (ShipRise) GoGaRuCo 2012 45 / 50
Code to Joy Subclassing Module Subclassing Module • Creates a new “kind” of module • Enables us to add state to the modules a class includes • Enabled us to implement a new type of message delegation in Ruby Avdi Grimm (ShipRise) GoGaRuCo 2012 46 / 50
Code to Joy Conclusion Sometimes we get discouraged • Technical debt got you down? • Community drama bumming you out? Avdi Grimm (ShipRise) GoGaRuCo 2012 48 / 50
Code to Joy Conclusion Sometimes we get discouraged • Technical debt got you down? • Community drama bumming you out? • If you’re blue, don’t know where to go to. . . Avdi Grimm (ShipRise) GoGaRuCo 2012 48 / 50
Code to Joy Conclusion Sometimes we get discouraged • Technical debt got you down? • Community drama bumming you out? • If you’re blue, don’t know where to go to. . . • . . . practice joyful coding Avdi Grimm (ShipRise) GoGaRuCo 2012 48 / 50
Code to Joy Conclusion Sometimes we get discouraged • Technical debt got you down? • Community drama bumming you out? • If you’re blue, don’t know where to go to. . . • . . . practice joyful coding • How? Avdi Grimm (ShipRise) GoGaRuCo 2012 48 / 50
Code to Joy Conclusion Defend your joy • There’s no metric for joyful code • Shared code is joyful code • If you want to feel good about your craft. . . Avdi Grimm (ShipRise) GoGaRuCo 2012 49 / 50
Code to Joy Conclusion Defend your joy • There’s no metric for joyful code • Shared code is joyful code • If you want to feel good about your craft. . . • . . . give someone a “wow” moment. Avdi Grimm (ShipRise) GoGaRuCo 2012 49 / 50
Code to Joy Conclusion Defend your joy • There’s no metric for joyful code • Shared code is joyful code • If you want to feel good about your craft. . . • . . . give someone a “wow” moment. • Show one person something awesome. Avdi Grimm (ShipRise) GoGaRuCo 2012 49 / 50
Code to Joy Conclusion Defend your joy • There’s no metric for joyful code • Shared code is joyful code • If you want to feel good about your craft. . . • . . . give someone a “wow” moment. • Show one person something awesome. • I promise you’ll feel better! Avdi Grimm (ShipRise) GoGaRuCo 2012 49 / 50