module Toolkit::String

Public Class Methods

safe(str) click to toggle source

终极解决方案 遇到了 invalid byte sequence in UTF-8 (ArgumentError) 问题 解决办法参考 stackoverflow.com/questions/29877310/invalid-byte-sequence-in-utf-8-argumenterror

# File lib/string.rb, line 8
          def safe(str)
                  case OS.os_family
                  when "unix"
                          if ! str.valid_encoding?
                                  str = str.encode("UTF-16be", :invalid=>:replace, :replace=>"?").encode('UTF-8')
                          end 
                  when "windows"
                          #str = str.force_encoding("UTF-8")
                          str = str.encode("UTF-16be", :invalid=>:replace, :replace=>"?").encode('UTF-8')
                  end
return str 
          end
safe_path(path) click to toggle source

改写成安全的Windows书写方式

# File lib/string.rb, line 22
          def safe_path(path)
if OS.windows?
  path.gsub!("/", "\\")
end
          end