Swapping letters in a string in Ruby -


i need swap letters in string (dna strand) using ruby , following rules:

  • 'a' replaced 't'
  • 't' replaced 'a'
  • 'c' replaced 'g'
  • 'g' replaced 'c'

for example, 'acgta' should become 'tgcat'.

i have got far:

def dna_strand(dna)       dna.tr!('a', 't')     end 

you quite close:

dna.tr('atcg', 'tagc')   # => "tgcat" 

see ruby-doc.org on tr:

returns copy of str characters in from_str replaced corresponding characters in to_str.

use tr! same way if want modify string in-place.


Comments