Skip to content

Instantly share code, notes, and snippets.

@pwnall
Created September 14, 2013 08:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save pwnall/6559814 to your computer and use it in GitHub Desktop.
Save pwnall/6559814 to your computer and use it in GitHub Desktop.
Test whether a Ruby VM has the hooks we need to implement label propagation for Police.
class LabeledString < String
def +(other)
LabeledString.new original_plus(other)
end
end
class String
alias_method :original_plus, :+
def +(other)
if other.instance_of? LabeledString
LabeledString.new original_plus(other.to_s)
else
original_plus other
end
end
end
class Regexp
alias_method :original_match, :match
def match(string, pos=0)
if string.instance_of? LabeledString
if pos == 0
match_data = original_match string
else
match_data = original_match string, pos
end
if match_data.nil?
match_data
else
WrappedMatchData.new match_data
end
else
if pos == 0
original_match string
else
original_match string, pos
end
end
end
end
class WrappedMatchData
def initialize(match_data)
@match_data = match_data
end
def [](index)
string = @match_data[index]
if string.nil?
string
else
LabeledString.new string
end
end
end
s1 = LabeledString.new "Sekret Name"
s2 = "Public Name"
# Preface output with the VM name.
p RUBY_DESCRIPTION
# This works.
p [(s1 + s2).class, LabeledString]
p [(s2 + s1).class, LabeledString]
p [(s1 + s1).class, LabeledString]
p [(s2 + s2).class, String]
# This needs string interpolation changes to work.
p ["Hi #{s1}".class, LabeledString]
p ["Hi #{s2}".class, String]
# This works.
p [/(name)/i.match(s1)[1].class, LabeledString]
p [/(name)/i.match(s2)[1].class, String]
# This works, but I'm not sure it should.
/(name)/i =~ s1
p [$1.class, LabeledString]
p [$~[1].class, LabeledString]
/(name)/i =~ s2
p [$1.class, String]
p [$~[1].class, String]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment