module Comodule::CustomizeClass::StringCustom
Public Instance Methods
ascii_space()
click to toggle source
# File lib/comodule/customize_class/string_custom.rb, line 39 def ascii_space gsub(/\p{Z}/u, ?\s) end
ascii_space!()
click to toggle source
# File lib/comodule/customize_class/string_custom.rb, line 43 def ascii_space! str = ascii_space return nil if str == self replace str self end
digitalize()
click to toggle source
# File lib/comodule/customize_class/string_custom.rb, line 118 def digitalize standardize.gsub(/[^0-9]/,"") end
digitalize!()
click to toggle source
# File lib/comodule/customize_class/string_custom.rb, line 122 def digitalize! str = digitalize return nil if str == self replace str self end
ltrim()
click to toggle source
# File lib/comodule/customize_class/string_custom.rb, line 6 def ltrim lstrip.sub(/^\p{Z}+/mu, '') end
ltrim!()
click to toggle source
# File lib/comodule/customize_class/string_custom.rb, line 10 def ltrim! str = ltrim return nil if str == self replace str self end
rtrim()
click to toggle source
# File lib/comodule/customize_class/string_custom.rb, line 17 def rtrim rstrip.sub(/\p{Z}+$/mu, '') end
rtrim!()
click to toggle source
# File lib/comodule/customize_class/string_custom.rb, line 21 def rtrim! str = rtrim return nil if str == self replace str self end
single_space()
click to toggle source
全角または半角のスペースが連続する場合は一つの半角スペースにする。
# File lib/comodule/customize_class/string_custom.rb, line 51 def single_space trim.gsub(/\p{Z}+/u, ?\s) end
single_space!()
click to toggle source
# File lib/comodule/customize_class/string_custom.rb, line 55 def single_space! str = single_space return nil if str == self replace str self end
standardize(_single_space=single_space)
click to toggle source
NKF.nkfで“½”などの文字が失われることがあるので、文字数が変化してしまったら一文字ずつの変換を行う。 半角カタカナの濁点、半濁点により文字数が変化した場合も一文字ずつの処理にする。
# File lib/comodule/customize_class/string_custom.rb, line 64 def standardize(_single_space=single_space) before = _single_space || trim.ascii_space after = NKF.nkf( '-Wwxm0Z0', NKF.nkf('-WwXm0', before) ) before.size == after.size ? after : standardize_delicate(_single_space) end
standardize!()
click to toggle source
# File lib/comodule/customize_class/string_custom.rb, line 94 def standardize! str = standardize return nil if str == self replace str self end
standardize_delicate(_single_space=single_space)
click to toggle source
1文字ずつ変換するので、当然パフォーマンスが低い。
# File lib/comodule/customize_class/string_custom.rb, line 71 def standardize_delicate(_single_space=single_space) str = _single_space || trim.ascii_space str_array = [] # 濁点と半濁点が一文字として変換されることを避ける。 str_chars = str.chars.to_enum loop do s = str_chars.next # 濁点、半濁点は直前の文字と組み合わせる。 if !str_array.empty? && s =~ /(゙|゚)/ s = str_array.pop+s end str_array << s end re_str = "" str_array.each do |char| re_char = NKF.nkf( '-Wwxm0Z0', NKF.nkf('-WwXm0', char) ) re_str << (re_char.present? ? re_char : char) end re_str end
to_token(hsh={})
click to toggle source
空白文字をワイルドカードに置き換えて検索ワードを作る。 ex. “株 山 のり” -> “株%山%のり%” デフォルトは前方一致、部分一致にしたければ:prefixに“%”を渡す。
# File lib/comodule/customize_class/string_custom.rb, line 104 def to_token(hsh={}) prefix = hsh[:prefix] || "" suffix = hsh[:suffix] || ?% str = sub(/^%+/,'').sub(/%+$/,'') prefix + str.split(/\p{Z}+/u).join(?%) + suffix end
to_token!(*args)
click to toggle source
# File lib/comodule/customize_class/string_custom.rb, line 111 def to_token!(*args) str = to_token(*args) return nil if str == self replace str self end
trim()
click to toggle source
# File lib/comodule/customize_class/string_custom.rb, line 28 def trim ltrim.rtrim end
trim!()
click to toggle source
# File lib/comodule/customize_class/string_custom.rb, line 32 def trim! str = trim return nil if str == self replace str self end