2012-07-04 3 views
3

문자열이있어서 다른 위치의 여러 문자를 대체하여 해당 문자열을 인쇄하려고합니다.Ruby의 인덱스 위치에서 대체 문자

예.

여기 나는 string_replace가있는 위치에서 문자열을 대체하려고합니다.

string = "AGACACTTTATATGTAT" 

positions = ["2", "5", "8", "10"] 

string_replace = ["T", "A", "G", "G"] 

내가 필요로 출력 내가이 시도이 => "AGTCAATTGAGATGTAT"

하지만 성공하지 :

positions.zip(string_replace).each do |pos, str| 
    string.gsub!(/#{string}[#{pos}]/, '#{str}') 
    puts string 
end 

모든 지원은 이해할 수있을 것이다. 여기

답변

6
positions.zip(string_replace).each do |pos, str| 
    string[pos.to_i] = str 
    puts string 
end 
1

:

positions.each_with_index {|o, i| string[o]=replacments[i]}