J
Joao Silva
My development envrioment:
~% ruby -v
ruby 1.8.7 (2008-08-11 patchlevel 72) [i686-darwin9]
My production envrioment:
x@x:~$ ruby -v
ruby 1.8.6 (2008-08-11 patchlevel 287) [i686-linux]
Ruby Enterprise Edition 20090610
My code:
# Convert comma separated string to array of Regexps & compiles to one
Regexp.
# From "test, /test/i, testoweX, test-*, te?st*, /te(.*)t/" makes
/(?-mix:test)|(?i-mx:test)|(?-mix:testoweX)|(?-mix:test-.*)|(?-mix:te.st.*)|(?-mix:te(.*)t)/
def negative_terms_regexp
buffer = []
negative_terms.split(',').each do |term|
term.strip!
if term.include_regexp?
buffer << term.to_regexp
else
if term.include?('*') || term.include?('?')
buffer << term.gsub("?", ".").gsub("*", ".*").to_regexp
else
buffer << Regexp.new(term)
end
end
end
Regexp.union(buffer.uniq)
end
Here is also my String class monkeypatch:
class String
# Does string containg regexp?
def include_regexp?
if /\A\/.*\/[iomx]*\Z/.match(self)
true
else
false
end
end
# Convert any String to Regexp.
# More info: http://www.ruby-forum.com/topic/193809
def to_regexp
return Regexp.new(self) unless
self.strip.match(/\A\/(.*)\/(.*)\Z/mx)
regexp , flags = $1 , $2
return nil if !regexp || flags =~ /[^xim]/m
x = /x/.match(flags) && Regexp::EXTENDED
i = /i/.match(flags) && Regexp::IGNORECASE
m = /m/.match(flags) && Regexp::MULTILINE
Regexp.new regexp , [x,i,m].inject(0){|a,f| f ? a+f : a }
end
end
This code in development works properly, but in production, i get array
of pure strings instead of array of regexp. What's wrong? Thanks!
~% ruby -v
ruby 1.8.7 (2008-08-11 patchlevel 72) [i686-darwin9]
My production envrioment:
x@x:~$ ruby -v
ruby 1.8.6 (2008-08-11 patchlevel 287) [i686-linux]
Ruby Enterprise Edition 20090610
My code:
# Convert comma separated string to array of Regexps & compiles to one
Regexp.
# From "test, /test/i, testoweX, test-*, te?st*, /te(.*)t/" makes
/(?-mix:test)|(?i-mx:test)|(?-mix:testoweX)|(?-mix:test-.*)|(?-mix:te.st.*)|(?-mix:te(.*)t)/
def negative_terms_regexp
buffer = []
negative_terms.split(',').each do |term|
term.strip!
if term.include_regexp?
buffer << term.to_regexp
else
if term.include?('*') || term.include?('?')
buffer << term.gsub("?", ".").gsub("*", ".*").to_regexp
else
buffer << Regexp.new(term)
end
end
end
Regexp.union(buffer.uniq)
end
Here is also my String class monkeypatch:
class String
# Does string containg regexp?
def include_regexp?
if /\A\/.*\/[iomx]*\Z/.match(self)
true
else
false
end
end
# Convert any String to Regexp.
# More info: http://www.ruby-forum.com/topic/193809
def to_regexp
return Regexp.new(self) unless
self.strip.match(/\A\/(.*)\/(.*)\Z/mx)
regexp , flags = $1 , $2
return nil if !regexp || flags =~ /[^xim]/m
x = /x/.match(flags) && Regexp::EXTENDED
i = /i/.match(flags) && Regexp::IGNORECASE
m = /m/.match(flags) && Regexp::MULTILINE
Regexp.new regexp , [x,i,m].inject(0){|a,f| f ? a+f : a }
end
end
This code in development works properly, but in production, i get array
of pure strings instead of array of regexp. What's wrong? Thanks!