Created
November 28, 2012 11:55
-
-
Save qickstarter/4160708 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# coding: utf-8 | |
module Kernel | |
alias_method :old_puts, :puts | |
# putsをフック | |
# Stringに対しては自動的にto_s呼ばないのね... | |
def puts(*args) | |
args.map! { |obj| obj.to_s } | |
old_puts(args) | |
end | |
end | |
class String | |
# before_filter_methodでフック | |
def self.before_fook_method( before_filter_method, method ) | |
original_method = "old_#{method}" | |
# original_method = "old_#{method}".to_sym # XXX これだとエラーになる... | |
# hook_method | |
alias_method( original_method, method ) | |
define_method method do |*args| | |
send( before_filter_method, *args ) | |
send( original_method, *args ) | |
end | |
end | |
before_fook_method :pinch2chance, :to_sym | |
before_fook_method :pinch2chance, :inspect | |
before_fook_method :pinch2chance, :to_s | |
private | |
def pinch2chance | |
gsub!('ピンチ', 'チャンス') | |
end | |
end | |
puts "ピンチ" | |
p "ピンチでしかない" | |
p "ピンチ" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment