次のロジックをRailsの文字列に適用する簡単な方法はありますか?もちろんRailsで文字列の開始または終了を確認する
if string does NOT end with '.' then add '.'
if string does NOT begin with 'http://' then add 'http://'.
、1のような何かするかもしれない:
string[0..6] == 'http://' ? nil : (string = 'http://' + string)
をしかし、それは少し不格好であり、それはhttps://
代替を欠場だろう。これを行うためのより良いRailsの方法はありますか?
string = "#{string}." unless string.match(/\.$/)
string = "http://#{string}" unless string.match(/^https?:\/\//)
のようなものが動作するはず