Ruby regular expression using variable name

后端 未结 5 1607
难免孤独
难免孤独 2020-11-28 04:08

Is is possible to create/use a regular expression pattern in ruby that is based on the value of a variable name?

For instance, we all know we can do the following wi

5条回答
  •  猫巷女王i
    2020-11-28 04:46

    It works, but you need to use gsub! or assign the return to another variable

    var = "Value"
    str = "a test Value"
    str.gsub!( /#{var}/, 'foo' )  # Or this: new_str = str.gsub( /#{var}/, 'foo' )
    puts str
    

提交回复
热议问题