Created
June 19, 2012 06:13
-
-
Save kwappa/2952564 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 -*- | |
require 'rspec' | |
class String | |
def mb_width | |
self.split('').inject(0) { |r, c| c =~ /^[ -~。-゚]*$/ ? r += 1 : r += 2 ; r } | |
end | |
end | |
describe String do | |
context '#mb_width' do | |
specify "should return 4 if 'hoge' given" do | |
"hoge".mb_width.should == 4 | |
end | |
specify "should return 11 if 'ほげandぴよ' given" do | |
"ほげandぴよ".mb_width.should == 11 | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
6行目、最後にr返さなくてもいいのか。