Skip to content

Instantly share code, notes, and snippets.

@GUI
Last active August 29, 2015 13:57
Show Gist options
  • Save GUI/9584219 to your computer and use it in GitHub Desktop.
Save GUI/9584219 to your computer and use it in GitHub Desktop.
def foo
if("moo" =~ /(\w+)/)
puts " inside foo: #{$1.inspect} #{Regexp.last_match.inspect}"
end
end
def replace
"something".gsub(/(foo)/, "bar")
end
class String
def replace_test
self.gsub(/(foo)/, "bar")
end
end
def bar
if("hello" =~ /(\w+)/)
puts "before foo: #{$1.inspect} #{Regexp.last_match.inspect}"
foo
puts "after foo: #{$1.inspect} #{Regexp.last_match.inspect}"
5.to_r
puts "after Integer#to_r: #{$1.inspect} #{Regexp.last_match.inspect}"
0.25.to_r
puts "after Float#to_r: #{$1.inspect} #{Regexp.last_match.inspect}"
replace
puts "after replace: #{$1.inspect} #{Regexp.last_match.inspect}"
".25".replace_test
puts "after String#replace_test: #{$1.inspect} #{Regexp.last_match.inspect}"
".25".to_r
puts "after String#to_r: #{$1.inspect} #{Regexp.last_match.inspect}"
end
end
bar
# MRI ruby 1.9.3p545
# before foo: "hello" #<MatchData "hello" 1:"hello">
# inside foo: "moo" #<MatchData "moo" 1:"moo">
# after foo: "hello" #<MatchData "hello" 1:"hello">
# after Integer#to_r: "hello" #<MatchData "hello" 1:"hello">
# after Float#to_r: "hello" #<MatchData "hello" 1:"hello">
# after replace: "hello" #<MatchData "hello" 1:"hello">
# after String#replace_test: "hello" #<MatchData "hello" 1:"hello">
# after String#to_r: "hello" #<MatchData "hello" 1:"hello">
#
# MRI ruby 2.1.1p76
# before foo: "hello" #<MatchData "hello" 1:"hello">
# inside foo: "moo" #<MatchData "moo" 1:"moo">
# after foo: "hello" #<MatchData "hello" 1:"hello">
# after Integer#to_r: "hello" #<MatchData "hello" 1:"hello">
# after Float#to_r: "hello" #<MatchData "hello" 1:"hello">
# after replace: "hello" #<MatchData "hello" 1:"hello">
# after String#replace_test: "hello" #<MatchData "hello" 1:"hello">
# after String#to_r: "hello" #<MatchData "hello" 1:"hello">
#
# JRuby 1.7.11
# before foo: "hello" #<MatchData "hello" 1:"hello">
# inside foo: "moo" #<MatchData "moo" 1:"moo">
# after foo: "hello" #<MatchData "hello" 1:"hello">
# after Integer#to_r: "hello" #<MatchData "hello" 1:"hello">
# after Float#to_r: "hello" #<MatchData "hello" 1:"hello">
# after replace: "hello" #<MatchData "hello" 1:"hello">
# after String#replace_test: "hello" #<MatchData "hello" 1:"hello">
# after String#to_r: nil nil
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment