Skip to content

Instantly share code, notes, and snippets.

@znz
Created August 10, 2012 04:46
Show Gist options
  • Save znz/3311132 to your computer and use it in GitHub Desktop.
Save znz/3311132 to your computer and use it in GitHub Desktop.
$1 を使っている String#gsub を置き換えてみるテスト
% ruby -v gsub.rb
ruby 1.8.7 (2011-06-30 patchlevel 352) [x86_64-linux]
<a><b><c>
% ruby -v gsub.rb
ruby 1.9.3p194 (2012-04-20 revision 35410) [x86_64-linux]
<a><b><c>
if true
class String
alias _gsub gsub
def gsub(*args, &block)
_gsub(*args) do |*block_args|
$_match_data = $~
eval("$~ = $_match_data", block.binding)
block.call(*block_args)
end
end
end
end
s = "abc".gsub(/(.)/) do
"<#{$1}>"
end
puts s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment