
-----------------------------------
Cervantes
Fri Jan 13, 2006 9:26 pm

DNA to RNA to Protein
-----------------------------------
This code allows a biology student to take a nucleotide sequence (of DNA), convert it to RNA, then convert that to an amino acid sequence: a protein.


class Array
	def each_group( group_length, &block )
		self.length.div(group_length).times do |i|
			yield self 

For a bigger nucleotide sequence, try the attached file and switch the commenting at the end of the program.

-----------------------------------
wtd
Fri Jan 13, 2006 11:29 pm


-----------------------------------
class Array
        def each_group( group_length, &block )
                self.length.div(group_length).times do |i|
                        yield self [i * group_length .. (i + 1) * group_length - 1]
                end
        end 

Why does each_group accept &block?  This parameter is not used in the method.

-----------------------------------
wtd
Fri Jan 13, 2006 11:34 pm


-----------------------------------
Also, when you have a method that takes a single argument, you can ellide the parentheses.  This is often used to great effect when the method ends in a question mark.

-----------------------------------
Cervantes
Sat Jan 14, 2006 11:09 am


-----------------------------------
class Array
        def each_group( group_length, &block )
                self.length.div(group_length).times do |i|
                        yield self [i * group_length .. (i + 1) * group_length - 1]
                end
        end 

Why does each_group accept &block?  This parameter is not used in the method.

Thanks for pointing that out.  I had thought I needed it as a way to pass the block to each_group from each_codon, but apparently not: I can still call each_group from each_codon with

@sequence.each_group( 3, &block )


Thanks. :)

-----------------------------------
Andy
Thu Feb 02, 2006 9:44 am


-----------------------------------
wow.. believe it or not, that was part of an assignment in cs134 at waterloo.. well the assignment had alot more to it.. but this was definitely in it

-----------------------------------
wtd
Thu Feb 02, 2006 2:17 pm


-----------------------------------
class Array
        def each_group( group_length, &block )
                self.length.div(group_length).times do |i|
                        yield self [i * group_length .. (i + 1) * group_length - 1]
                end
        end 

Why does each_group accept &block?  This parameter is not used in the method.

In fact, let's clean that up some more.

def each_group(group_length)
   (length / group_length).times do |i|
      yield self[i * group_length ... (i + 1) * group_length]
   end
end 
