Permalink
Browse files

In which I show off my poor knowledge of biology

What we're trying to capture here is what DNA turns into when it becomes
RNA, and vice versa, right? I'm a little vague on the biology,
admittedly. This whole 'complement' thing seems like a different way of
saying Ruby's `to_` idiom. So, while keeping the current Complement API,
I'm going to rewrite the underling code to use `to_dna` and `to_rna`

Along with this, I'm reversing the responsibility for knowing
complements. Previously, RNA would check itself for a complement to one
of its own nucleotides, but I think the best way to model this is for it
to ask DNA. It's converting `to_dna` so it seems like DNA would know the
right nucleotide matches. But, again, I probably got a C- in High School Biology
  • Loading branch information...
Ian Whitney
Ian Whitney committed Dec 24, 2014
1 parent 09bc50b commit cce80eb93332cf4de38e5037f93a3d201b162d7f
Showing with 14 additions and 31 deletions.
  1. +14 −31 ruby/rna-transcription/complement.rb
@@ -1,16 +1,20 @@
class Complement
def self.of_dna(strand)
DNA.new(strand).complement
DNA.new(strand).to_rna
end
def self.of_rna(strand)
RNA.new(strand).complement
RNA.new(strand).to_dna
end
end
class RNA
attr_accessor :strand
def self.complement_for(nucleotide)
{"C" => "G", "G" => "C", "T" => "A", "A" => "U"}[nucleotide]
end
def initialize(strand)
self.strand = strand
end
@@ -19,29 +23,21 @@ def nucleotides
strand.chars
end
def complement
def to_dna
nucleotides.inject("") do |ret, nucleotide|
ret << complement_of(nucleotide)
ret << DNA.complement_for(nucleotide)
end
end
def complement_of(nucleotide)
case nucleotide
when 'C'
'G'
when 'G'
'C'
when 'U'
'A'
when 'A'
'T'
end
end
end
class DNA
attr_accessor :strand
def self.complement_for(nucleotide)
{"C" => "G", "G" => "C", "U" => "A", "A" => "T"}[nucleotide]
end
def initialize(strand)
self.strand = strand
end
@@ -50,22 +46,9 @@ def nucleotides
strand.chars
end
def complement
def to_rna
nucleotides.inject("") do |ret, nucleotide|
ret << complement_of(nucleotide)
end
end
def complement_of(nucleotide)
case nucleotide
when 'C'
'G'
when 'G'
'C'
when 'T'
'A'
when 'A'
'U'
ret << RNA.complement_for(nucleotide)
end
end
end

0 comments on commit cce80eb

Please sign in to comment.