Last active
August 29, 2015 14:07
-
-
Save danielpclark/c414a3e0dd9a7df767b6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Code exercise in refactoring from a code sample from @brianllamar | |
module DNA | |
module Hamming | |
def self.compute(pair) | |
set_strand_position_by_length(pair) unless pair.same? | |
count_the_distance(pair) | |
end | |
private | |
def self.count_the_distance(pair) | |
pair.entries.map(&:chars).inject(:zip).select{|a,b| a!=b}.count | |
end | |
def self.set_strand_position_by_length(pair) | |
pair.swap! if pair.inject{|a,b|a.length>b.length} | |
end | |
end | |
class Pair < Struct.new(:first, :second) | |
def same? | |
inject(:eql?) | |
end | |
def swap! | |
members.zip(entries.reverse) {|a,b| self[a] = b} | |
end | |
end | |
end | |
dna = lambda {|qty| "ACTG".chars.cycle.take(qty).shuffle.join} | |
pair = DNA::Pair.new(dna[10],dna[12]) | |
puts pair.entries | |
puts DNA::Hamming.compute(pair) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
private
is not a block and therefore indentation doesn't apply. It, by itself, modifies any methods below to private methods. It can also be used inline for just one methodprivate def my_private_method
. It's a modifier for methods... you also havepublic
which you can use in the same manner.