=~ /\A#!/ first_line = content.lines.take(2).last else first_line = content.lines.first end comment = first_line.sub( /(?:\"(?:[^"]|\\\")*\"|\'(?:[^']|\\\')*\'|[^#]*)#/, '').strip comment =~ /\b(?:en)?coding\s*:\s*(?:utf|UTF)-?8\b/ end def insert_magic_comment(path) content = open(path, 'rb') {|io| io.read } rescue $! return if Exception === content || content.empty? content.force_encoding('BINARY') if content.respond_to?(:force_encoding) unless has_magic_comment?(content) if content =~ /[^\x00-\x7E]/m $stderr.puts "inserting magic comment to #{path}" open(path, 'wb') do |io| io.puts "# coding: utf-8" io.write content end end end end if ARGV[0] == '--pre-commit' open("|git diff --cached --name-only HEAD") do |io| while path = io.gets path.strip! next unless path =~ /\.rb$/ insert_magic_comment(path) end end else require 'find' Find.find(Dir.pwd) do |path| next unless path =~ /\.rb$/ insert_magic_comment(path) end end 14
|callstack| repository_root = File.expand_path( '../../../../../../../', __FILE__) + '/' offending_line = callstack.find {|line| File.expand_path(line.split(':').first). start_with?(repository_root) } || callstack.first $stderr.puts "[WARN] can't iterate from Time since 1.9 at #{offending_line}" end end include_without_warn?(obj) end alias include_without_warn? include? alias include? include_with_warn? end 41
args[1] || args[1].nil? $stderr.puts "\n[WARN] non-Numeric object is given for the 2nd argument of step at #{caller[0]}" $stderr.flush end step_without_warn(*args, &block) end alias step_without_warn step alias step step_with_warn end 44
/\A(?:#{UTF8_WHITESPACE_CHAR_PATTERN})*\z/.freeze end # for 1.9.3 class String NON_BLANK_SPACE_CHARACTERS = "\u00A0\u1680\u180E\u2000-\u200B\u202F\u205F".freeze private_constant :NON_BLANK_SPACE_CHARACTERS UTF8_WHITESPACE_CHAR_PATTERN = /(?:(?![#{NON_BLANK_SPACE_CHARACTERS}])\p{Space})/.freeze UTF8_BLANK_PATTERN = /\A(?:#{UTF8_WHITESPACE_CHAR_PATTERN})*\z/.freeze end 79
-x’, self) end end # Ruby 1.9.3 class String def to_win31j_from_utf8 encode('Windows-31J', 'UTF-8', invalid: :replace, undef: :replace, replace: '') end end 82
#{caller[0]}” tosjis_without_warn end alias tosjis_without_warn tosjis alias tosjis tosjis_with_warn end # Ruby 1.9.3 class String def tosjis $stderr.puts “[WARN] ... #{caller[0]}” encode(‘Windows-31J’, invalid: :replace, undef: :replace, replace: '') end end 84
NilClass def id_with_warn(*args) return 4 unless File.expand_path(caller[0]). starts_with?(Rails.root) message = "nil.id was called at #{caller[0]}" if defined? Logger Logger.error.post('nil.id', message) else $stderr.puts message end 4 end alias id_without_warn id alias id id_with_warn end end 88
ruby first Dir[‘monkey_patches/ruby/**/*.rb’].sort.each do |fn| version = fn.split(‘/’)[2] case when version == ‘common’ # do nothing when RUBY_VERSION < ‘1.9’ next unless version == ‘1.8’ when RUBY_VERSION < ‘2.0’ next unless version == ‘1.9’ when RUBY_VERSION < ‘2.1’ next unless (‘1.9’...‘2.1’).cover? version else next unless version >= ‘2.1’ end require fn end end 103
name == :force_encoding self else method_missing_without_force_encoding( name, *args, &block) end end alias method_missing_without_force_encoding method_missing alias method_missing method_missing_with_force_encoding end 115