-2

I have a code:

require 'pp'

def unquote_string(string)
    if (string.is_a?(String))
        string.gsub(/\\/,'')
    else 
        string
    end
end

def filter_array_with_substitution_and_replacement(array,options={})
    pp options
    return array unless %w(filterRegex substitudeRegex replacementExpression).any? {|key| options.has_key? key}
    puts "I will substitude!"
    filterRegex = options["filterRegex"]
    substitudeRegex = options["substitudeRegex"]
    replacementExpression = options["replacementExpression"]
    pp "I have: #{replacementExpression}"
    array.select{|object| 
        object =~ filterRegex
    }.map!{|object| 
        object.sub!(substitudeRegex,unquote_string(replacementExpression))
    }
end


def sixth_function
    array = %w(onetwo onethree onefour onesix none any other)
    filterRegex = /one/
    substitudeRegex = /(one)(\w+)/
    replacementExpression = '/#{$2}.|.#{$1}/'
    options = {
        "filterRegex" => filterRegex,
        "substitudeRegex" => substitudeRegex,
        "replacementExpression" => replacementExpression
    }   
    filter_array_with_substitution_and_replacement(array,options)
    pp array
end

def MainWork
   sixth_function()
end

MainWork()

Output:

{"filterRegex"=>/one/,
 "substitudeRegex"=>/(one)(\w+)/,
 "replacementExpression"=>"/\#{$2}.|.\#{$1}/"}
I will substitude!
"I have: /\#{$2}.|.\#{$1}/"
smb192168164:Scripts mac$ ruby rubyFirst.rb
{"filterRegex"=>/one/,
 "substitudeRegex"=>/(one)(\w+)/,
 "replacementExpression"=>"/\#{$2}.|.\#{$1}/"}
I will substitude!
"I have: /\#{$2}.|.\#{$1}/"
["/\#{$2}.|.\#{$1}/",
 "/\#{$2}.|.\#{$1}/",
 "/\#{$2}.|.\#{$1}/",
 "/\#{$2}.|.\#{$1}/",
 "none",
 "any",
 "other"]

It is not correct, because string with replacement have metacharacters quoted. how to correct unquote this string replacementExpression?

Desired output for array after replacement:

["two.|.one/",
 "three.|.one/",
 "four.|.one/",
 "six.|.one/",
 "none",
 "any",
 "other"]
gaussblurinc
  • 3,642
  • 9
  • 35
  • 64
  • What is your desired output? – Chowlett Mar 05 '14 at 14:37
  • As an aside, you should pick a more useful name for `sixth_function()`. – Josh Mar 05 '14 at 14:51
  • @Josh This is my first ruby file, where I test everything. Yes, I must call it like "test_one" or "test_two" if you want – gaussblurinc Mar 05 '14 at 14:52
  • @DaniëlKnippers, so, how to solve this task with unquoting? Chowlett's \1 capture groups work well – gaussblurinc Mar 05 '14 at 14:53
  • @DaniëlKnippers, I try this approach. It doesn't work – gaussblurinc Mar 05 '14 at 15:34
  • @DaniëlKnippers, please, run my code on your machine or [see this link](http://codepad.org/pU9RVOY6) – gaussblurinc Mar 05 '14 at 15:45
  • @DaniëlKnippers, ok, let's stop :) please, see your example and my example. It is not the same, because in my case replacementString are passed as parameter. of course, your example will work, because it replace variables `$1` and `$2` (capture variables) in the place. – gaussblurinc Mar 05 '14 at 16:02
  • @gaussblurinc Yes correct, I forgot to mention that but you insert `$2` and `$1` into a String in another location but that won't work since they are global variables :). I think i'll remove most of my comments to clear this space up in a couple minutes. – Daniël Knippers Mar 05 '14 at 16:05

1 Answers1

1

Forget unquote_string - you simply want your replacementExpression to be '\2.|.\1':

"onetwo".sub(/(one)(\w+)/, '\2.|.\1')
#=> "two.|.one"
Chowlett
  • 45,935
  • 20
  • 116
  • 150