Advertisement
Guest User

Unquoting issue

a guest
Mar 5th, 2014
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rails 1.55 KB | None | 0 0
  1. require 'pp'
  2.  
  3. def unquote_string(string)
  4.     if (string.is_a?(String))
  5.         string.gsub(/\\/,'')
  6.     else
  7.         string
  8.     end
  9. end
  10.  
  11. def filter_array_with_substitution_and_replacement(array,options={})
  12.     pp options
  13.     return array unless %w(filterRegex substitudeRegex replacementExpression).any? {|key| options.has_key? key}
  14.     puts "I will substitude!"
  15.     filterRegex = options["filterRegex"]
  16.     substitudeRegex = options["substitudeRegex"]
  17.     replacementExpression = options["replacementExpression"]
  18.     pp "I have: #{replacementExpression}"
  19.     array.select{|object|
  20.         object =~ filterRegex
  21.     }.map!{|object|
  22.         object.sub!(substitudeRegex,unquote_string(replacementExpression))
  23.     }
  24. end
  25.  
  26.  
  27. def sixth_function
  28.     array = %w(onetwo onethree onefour onesix none any other)
  29.     filterRegex = /one/
  30.     substitudeRegex = /(one)(\w+)/
  31.     replacementExpression = '/#{$2}.|.#{$1}/'
  32.     options = {
  33.         "filterRegex" => filterRegex,
  34.         "substitudeRegex" => substitudeRegex,
  35.         "replacementExpression" => replacementExpression
  36.     }  
  37.     filter_array_with_substitution_and_replacement(array,options)
  38.     pp array
  39. end
  40.  
  41.  
  42. Output:
  43.  
  44. {"filterRegex"=>/one/,
  45.  "substitudeRegex"=>/(one)(\w+)/,
  46.  "replacementExpression"=>"/\#{$2}.|.\#{$1}/"}
  47. I will substitude!
  48. "I have: /\#{$2}.|.\#{$1}/"
  49. smb192168164:Scripts mac$ ruby rubyFirst.rb
  50. {"filterRegex"=>/one/,
  51.  "substitudeRegex"=>/(one)(\w+)/,
  52.  "replacementExpression"=>"/\#{$2}.|.\#{$1}/"}
  53. I will substitude!
  54. "I have: /\#{$2}.|.\#{$1}/"
  55. ["/\#{$2}.|.\#{$1}/",
  56.  "/\#{$2}.|.\#{$1}/",
  57.  "/\#{$2}.|.\#{$1}/",
  58.  "/\#{$2}.|.\#{$1}/",
  59.  "none",
  60.  "any",
  61.  "other"]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement