class Range
def include_with_warn?(obj)
if Time === self.begin
caller.tap do |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
Slide 47
Slide 47 text
Date#step ͕
ActiveSupport::Duration Λ
ڋઈͩ͢͠
42
Slide 48
Slide 48 text
# Ruby 1.9.3 Ͱಈ͔ͳ͘ͳͬͨίʔυ
begin_date = Date.parse(“2013/03/01”)
end_date = Date.parse(“2013/06/30”)
begin_date.step(end_date, 1.week) do |date|
...
end
43
Slide 49
Slide 49 text
require 'date'
class Date
def step_with_warn(*args, &block)
unless Numeric === 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
if RUBY_VERSION >= ‘1.9’
word = ‘\p{Word}’
re = /(?!#{word})./
else
re = /\W/
end
75
Slide 81
Slide 81 text
ಠࣗͷ
String#blank?
76
Slide 82
Slide 82 text
class String
def blank?
self =~ /\A[\sɹ]*\z/m
end
end
↑ IDEOGRAPHIC SPACE
77
Slide 83
Slide 83 text
IDEOGRAPHIC SPACE
ͷํ͡Όͳͯ͘ \s ʹ͋Δ
78
Slide 84
Slide 84 text
# for 1.8
class String
UTF8_WHITESPACE_CHAR_PATTERN =
/(?:\s|\xE3\x80\x80)/.freeze
UTF8_BLANK_PATTERN =
/\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
Slide 85
Slide 85 text
จࣈΤϯίʔσΟϯάมܥ
80
Slide 86
Slide 86 text
NKF ޓϝιου
81
Slide 87
Slide 87 text
# Ruby 1.8.7
class String
def to_win31j_from_utf8
NKF.nkf(‘-W -s -m0 -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
Slide 88
Slide 88 text
kconv ͷϝιουͰ
ܯࠂΛग़͢
83
Slide 89
Slide 89 text
# Ruby 1.8.7
class String
def tosjis_with_warn
$stderr.puts “[WARN] ... #{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
Slide 90
Slide 90 text
ܯࠂΛग़͢Α͏ʹͯ͠
͓͘ࣄͰɺαʔϏε
։ൃΤϯδχΞ͕ؾ
͍ͯमਖ਼ͯ͘͠ΕΔ
85
Slide 91
Slide 91 text
ActiveRecord ͱ
nil.id
86
Slide 92
Slide 92 text
1.8 Ͱ nil.id ͕ 4 Λ
ฦ͢ͷͰྫ֎͕ग़ͳ͍
87
Slide 93
Slide 93 text
if Rails.application.config.whiny_nils
require 'active_support/whiny_nil'
end
if RUBY_VERSION < '1.9'
class 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
Slide 94
Slide 94 text
ϝʔϧؔ
89
Slide 95
Slide 95 text
ଧ tmail
90
Slide 96
Slide 96 text
tmail → mail.gem
91
Slide 97
Slide 97 text
ۤߦ
92
Slide 98
Slide 98 text
tmail ͷόάʹґଘ
ͨ͠ίʔυ͕͋ͬͨ
93
Slide 99
Slide 99 text
·ͩઓ͍ऴΘͬͯ
ͳ͍ɾɾɾ
94
Slide 100
Slide 100 text
ײ
95
Slide 101
Slide 101 text
ྑ͘ͳ͍ίʔυΛېࢭ
͢Δඇޓੑେܴ
96
Slide 102
Slide 102 text
items.each do |@item|
97
Slide 103
Slide 103 text
ศརͩͬͨࣄ͕Ͱ͖
ͳ͘ͳΔͱ൵͍͠
98
Slide 104
Slide 104 text
# Ruby 1.9.3 Ͱಈ͔ͳ͘ͳͬͨίʔυ
begin_date = Date.parse(“2013/03/01”)
end_date = Date.parse(“2013/06/30”)
begin_date.step(end_date, 1.week) do |date|
...
end
99
# config/initializers/000_load_libs.rb
FileUtils.chdir(‘../../../lib’, __FILE__) do
# load monkey patches for 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
Slide 109
Slide 109 text
৽͍͠όʔδϣϯͰ
૿͑Δϝιου
ύονͰಋೖ͢Δ
104
Slide 110
Slide 110 text
Object#singleton_c
lass
105
Slide 111
Slide 111 text
# for 1.8
class Object
def singleton_class
class << self
self
end
end
end
106
Slide 112
Slide 112 text
String#bytesize
107
Slide 113
Slide 113 text
# for 1.8
class String
alias bytesize size
end
108
Slide 114
Slide 114 text
String#b
109
Slide 115
Slide 115 text
# for 1.8
class String
def b
self
end
end
# for 1.9
class String
def b
dup.force_encoding(‘ASCII-8BIT’)
end
end
110
Slide 116
Slide 116 text
String#force_enco
ding
111
Slide 117
Slide 117 text
# for 1.8
class String
def force_encoding(enc)
self
end
end
112
# for 1.8
class String
def method_missing_with_force_encoding(name, *args, &block)
if 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