简体   繁体   中英

Ruby use gsub within string interpolation

I'm trying to use gsub within a string interpolation

puts "Some words #{Lookup1[input[2]].gsub("[", "(")} some more words"

The input array looks like this:

input = [0,22,["Accounting","Customer Relations"],nil,nil,nil,nil]

My goal is to replace [] with ()

Thanks

假设您要更改input [1]数组的字符串表示形式:

puts "Some words #{Lookup1[input[2]].inspect.tr("[]", "()")} some more words"
> puts "Some words #{input[2].to_s.gsub('[','(').gsub(']', ')')} some more words"
#=> Some words ("Accounting", "Customer Relations") some more words

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM