Created
May 19, 2010 01:56
-
-
Save tobynet/405853 to your computer and use it in GitHub Desktop.
RailsのActiveSupportのalias_method_chainを使ったRubyでの特異メソッドの上書き方法
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
#!ruby -Ku | |
# override_method/stdout_hook_singleton_method_with_activesupport.rb | |
$KCODE = 'u' | |
# sample of override singleton method with active_supportlike a Rails | |
# alias_method_chain (ActiveSupport::CoreExtensions::Module) - APIdock | |
# http://apidock.com/rails/ActiveSupport/CoreExtensions/Module/alias_method_chain | |
require "rubygems" | |
require "active_support" | |
require "kconv" | |
$stdout.sync = true | |
class << $stdout | |
def write_with_for_sjis_filter(str) | |
#old_write_for_sjis_filter str.to_s.kconv(Kconv::SJIS, Kconv::UTF8) | |
#old_write_for_sjis_filter str.to_s.tosjis | |
# change to sjis AS IS | |
write_without_for_sjis_filter NKF.nkf('-sxm0', str.to_s) | |
end | |
# same as... | |
# alias_method :write_without_for_sjis_filter, :write | |
# alias_method :write, :write_with_for_sjis_filter | |
alias_method_chain :write, :for_sjis_filter | |
end | |
puts "表が怖い噂のソフト" | |
puts "UTF-8→SJISフックキタ━(゚∀゚)━ !!" | |
puts "▶✉✦❤➡✖✔✸●■◢ ◣ ◤ ◥◧ ◨ ◩ ◪▲▼▶◀♂ ♀ ♪ ♫ ☼" | |
puts "テストまげもりまげもり" | |
puts "お前の脳天などこのテポドンキックで粉々に粉砕してやるわアアア~~~~~~!" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
解説元サイト: •Rails風にActiveSupportのalias_method_chainを使う方法 - Ruby/特異メソッドの上書きと退避 - TOBY SOFT wiki