Created
February 18, 2013 02:59
-
-
Save takuya327/4974881 to your computer and use it in GitHub Desktop.
Rails 3.2.11
Bizarre behavior of UTF-8 string at validation of string length.
This file contains 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
# When value = 'トラッキングトラッキングトラッキングトラッキングトラッキングトラッキングトラッキングトラッキングトラッキングトラッキング' | |
validates :title, | |
:presence => true, | |
:length => { | |
:tokenizer => lambda{|value| | |
r = [] | |
r = value.each_byte{|n| | |
r << n | |
} | |
Rails.logger.debug value.bytesize # => 180 | |
Rails.logger.debug r.size # => 60 Why??? | |
r | |
}, | |
:maximum => 64 | |
} |
ちょっとideoneがつながらないのでこちらで失礼しますが、
!/usr/bin/ruby
coding:utf-8
puts RUBY_VERSION #1.9.3
s='トラッキングトラッキングトラッキングトラッキングトラッキングトラッキングトラッキングトラッキングトラッキングトラッキング'
puts s.bytesize #180
r = []
q = []
r = s.each_byte{|n|
r << n
q << n
}
puts r.size #60
puts r.class.name #String
puts q.size #180
puts q.class.name #Array
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
×:r = value.each_byte
○:value.each_byte