I've been going through some of the exercises over on exercism and this is one of my solutions: a basic RNA/DNA transcriber. I was happy enough at first but now, looking at it again, the solution looks very repetitive to me.
The two methods below are needed because there are tests to ensure we've implemented them. It's just the code inside the methods I'm trying to refactor.
def self.of_dna(dna) if dna.include? 'U' raise ArgumentError.new('Incorrect nucleotide detected. Please enter a DNA sequence.') else return dna.gsub(/[GCTA]/, 'G' => 'C', 'C' => 'G', 'T' => 'A', 'A' => 'U') end end def self.of_rna(rna) if rna.include? 'T' raise ArgumentError.new('Incorrect nucleotide detected. Please enter an RNA sequence') else return rna.gsub(/[GCAU]/, 'G' => 'C', 'C' => 'G', 'A' => 'T', 'U' => 'A') end end
As you can see, in one method I'm performing the following substitutions:
'G' for 'C' 'C' for 'G' 'A' for 'T' 'U' for 'A'
In the other method it's the reverse. Can anyone point me in the right direction towards simplifying this and making it more concise?