Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Ruby Refactoring

Ruby Refactoring

Talk I gave at a WomenWhoCode #Algorithms event!

Avatar for Meredith Edwards

Meredith Edwards

July 13, 2016
Tweet

More Decks by Meredith Edwards

Other Decks in Education

Transcript

  1. Refactoring for Readability How to write code other people can

    understand Meredith Edwards @meredith_marg
  2. Here’s An Example problem: Check to see if a string

    has the same number of 'x's and 'o's. Your method must return true or false and be case insensitive. The string can contain any character.
  3. Kinda Like This: XO("ooxx") => true XO("xooxx") => false XO("ooxXm")

    => true XO("zpzpzpp") => true // when no 'x' and 'o' is present should return true XO("zzoo") => false
  4. And an Example solution: def XO(str) xo_array = str.downcase.split("").select {

    |x| x == "x" || x == "o" } x_array = xo_array.partition { |x| x == "x" }.first o_array = xo_array.partition { |x| x == "x" }.last x_array.size == o_array.size end
  5. Refactoring Step #1: ANNotate! def XO(str) # downcase the string

    and split it into an array xo_array = str.downcase.split("").select { |x| x == "x" || x == "o" } ...
  6. ANNotate! ... # make an array of all the x's

    and call it x_array x_array = xo_array.partition { |x| x == "x" }.first # make an array of all the o's and call it o_array o_array = xo_array.partition { |x| x == "x" }.last ...
  7. ANNotate! ... # compare the size of x_array to o_array.

    return true if they're the same size, and false if they aren't. x_array.size == o_array.size end
  8. Step #2: Gather all your steps 1. Downcase the string

    and split it into an array 2. Make an array of all the x's and call it x_array 3. Make an array of all the o's and call it o_array 4. Compare the size of x_array to o_array. Return true if they're the same size, and false if they aren't.
  9. Step #3: Write Some new methods 1. Downcase the string

    and split it into an array def grab_xs_and_os(str) str.downcase.split("").select do |char| char == "x" || "o" end end
  10. Step #3: Write Some new methods 2. Make an array

    of all the x’s def grab_xs(array) array.partition{ |char| char == "x" }.first end
  11. Step #3: Write Some new methods 3. Make an array

    of all the o’s def grab_os(array) array.partition{ |char| char == "o" }.first end
  12. Step #3: Write Some new methods 4. Compare the size

    of x_array to o_array. Return true if they're the same size, and false if they aren't. def same_num_of_xs_os?(x_array, o_array) x_array.size == o_array.size end
  13. Step #4: put the pieces back together... def XO(str) xo_array

    = grab_xs_and_os(str) x_array = grab_xs(xo_array) o_array = grab_os(xo_array) same_num_of_xs_os?(x_array, o_array) end
  14. Take a second look... def XO(str) xo_arr = grab_xs_and_os(str) x_arr

    = grab_xs(xo_arr) o_arr = grab_os(xo_arr) same_num_of_xs_os?(x_arr, o_arr) end
  15. Why are we grabbing X’s twice ?? def XO(str) xo_array

    = grab_xs_and_os(str) x_array = grab_xs(xo_array) o_array = grab_os(xo_array) same_num_of_xs_os?(x_array, o_array) end
  16. We are Also grabbing O’s twice ! Oh boy. def

    XO(str) xo_array = grab_xs_and_os(str) x_array = grab_xs(xo_array) o_array = grab_os(xo_array) same_num_of_xs_os?(x_array, o_array) end
  17. Then use our new methods... def XO(str) x_array(str).size == o_array(str).size

    end def x_array(str) str.split("").select{|char|char == "x" || "X"} end def o_array(str) str.split("").select{|char|char == "o" || "O"} end
  18. But wait… are we making this too complicated? Why do

    we even need to make arrays for x’s and o’s?? Why can’t we just count the number of x’s in the string and compare that count to the number of o’s in the string??
  19. Hey, I think there’s a method for that! def XO(string)

    str = string.downcase str.count('x') == str.count('o') end
  20. Oh, and let’s give our ‘xo’ method a better name

    def XO(string) str = string.downcase str.count('x') == str.count('o') end
  21. Meredith Edwards Please get in touch! I’d love to hear

    from you. email: [email protected] twitter: @meredith_marg website: medwards1771.github.io/