我又寫道一個簡單PROGRAMM與所述用戶輸入內的(())來替換文本: 如果我有例如本這樣的文字:紅寶石代碼替換串
i hab an terrible ((userinput1)) last ((userinput2)) in a horrible ((userinput3))
我試圖第一替換( (以#{和))與}
str1 = gets.chomp
str2 = str1.clone
a = 0
begin
s = str2.index('((', a)
str2[s..s+1] = '#{'
a = a + s + 1
end until str2.length < a
b = 0
begin
s = str2.index('))', b)
str2[s..s+1] = '}'
b = b + s + 1
end until str2.length < b
userinput1 = gets.chomp
userinput2 = gets.chomp
userinput3 = gets.chomp
puts str2
但不知何故,紅寶石不要驗證userinputs,相反,我得到:
i hab an terrible #{userinput1} last #{userinput2} in a horrible #{userinput}
我認爲問題是,在我的代碼中寫道:的
str2[s..s+1] = '#{'
代替
str2[s..s+1] = "#{"
因爲所以我所有剩餘的代碼,直到它被封閉}的對象。那我該怎麼辦?我希望你能理解我的問題?
str1 = gets.chomp
word = str1.scan(/\(\(\w+\)\)/)
word.each do |word|
str1.gsub(word, "what the fuck")
end
puts str1
你需要'eval()'那個字符串或者什麼 – 2013-07-24 10:30:34
怎麼樣?我可以這樣做 –