Ruby/String/replace

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

Перейти к: навигация, поиск

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