use*_*130 0 ruby string replace gsub
这让我疯狂的时间远远超过应有的时间,我正在使用SIMPLE字符串替换,但它根据获得的信息无法替换字符串(在这种情况下,它是'url').
class Test
myURL = 'www.google.com'
puts 'Where are you from?'
location = gets
if location == 'England'
myURL['.com'] = '.co.uk'
elsif location == 'France'
myURL['.com'] = '.co.fr'
end
puts myURL
end
Run Code Online (Sandbox Code Playgroud)
我疯了吗?
更改location = gets到location = gets.chomp
发生的事情gets是它将您输入的所有内容都包含在包含Enter密钥的提示中.所以,如果你输入"英格兰",那么:
location == "England\n" #=> true
location == "England" #=> false
Run Code Online (Sandbox Code Playgroud)
该String#chomp方法将删除末尾的终止线.