Ruby/String/replace

Материал из Wiki.crossplatform.ru

(Различия между версиями)
Перейти к: навигация, поиск
м (1 версия)
 

Текущая версия на 17:56, 13 сентября 2010

Replace a substring

speaker = "King Richard, 2007"
speaker[", 2007"]= "III" # => "III"
p speaker # => "King Richard III"



The replace method replaces a string wholesale.

call = "All hands on deck!"
puts call.replace "All feet on deck!"



Use ERB to replace string

require "erb"
template = ERB.new %q{static string <%= food %>!}
food = "bacon"
template.result(binding)           
food = "peanut butter"
template.result(binding)           
puts template.result