Class: String
- Inherits:
-
Object
- Object
- String
- Defined in:
- lib/vmopt/ext/string_ext.rb
Overview
:nodoc: all
Instance Method Summary collapse
- #__camelize(first_letter_in_uppercase = true) ⇒ Object (also: #camelize)
- #__underscore ⇒ Object (also: #underscore)
- #to_arr ⇒ Object
- #to_gb2312(enc = 'UTF-8') ⇒ Object
-
#to_gbk(enc = 'UTF-8') ⇒ Object
TODO:参数功能目前没有用.
-
#to_utf8(enc = 'GBK') ⇒ Object
TODO:参数功能目前没有用.
-
#utf8? ⇒ Boolean
判断是否为utf8.
Instance Method Details
#__camelize(first_letter_in_uppercase = true) ⇒ Object Also known as: camelize
66 67 68 69 70 71 72 |
# File 'lib/vmopt/ext/string_ext.rb', line 66 def __camelize(first_letter_in_uppercase=true) if first_letter_in_uppercase self.to_s.gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase } else self.to_s[0].chr.downcase + camelize(self)[1..-1] end end |
#__underscore ⇒ Object Also known as: underscore
75 76 77 78 79 80 81 82 83 |
# File 'lib/vmopt/ext/string_ext.rb', line 75 def __underscore word = self.to_s.dup word.gsub!(/::/, '/') word.gsub!(/([A-Z]+)([A-Z][a-z])/,'\1_\2') word.gsub!(/([a-z\d])([A-Z])/,'\1_\2') word.tr!("-", "_") word.downcase! word end |
#to_arr ⇒ Object
62 63 64 |
# File 'lib/vmopt/ext/string_ext.rb', line 62 def to_arr() self.split('&') #暂时未考虑&在\之后的问题,fix this bug end |
#to_gb2312(enc = 'UTF-8') ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/vmopt/ext/string_ext.rb', line 47 def to_gb2312(enc = 'UTF-8') # require 'iconv' # Iconv.iconv('GB2312','UTF-8',self).at(0).to_s if self.respond_to?(:encode) self.encode('GB2312', enc) else begin self.unpack("U*") require 'iconv' if RUBY_VERSION.to_f < 1.9 Iconv.iconv('GB2312',enc,self).at(0).to_s rescue ::ArgumentError self end end end |
#to_gbk(enc = 'UTF-8') ⇒ Object
TODO:参数功能目前没有用
32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/vmopt/ext/string_ext.rb', line 32 def to_gbk(enc='UTF-8') if self.respond_to?(:encode) return self unless utf8? self.encode('GBK', enc) else return self unless utf8? require 'iconv' if RUBY_VERSION.to_f < 1.9 begin Iconv.iconv('GBK',enc,self).at(0).to_s rescue self end end end |
#to_utf8(enc = 'GBK') ⇒ Object
TODO:参数功能目前没有用
16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/vmopt/ext/string_ext.rb', line 16 def to_utf8(enc='GBK') if self.respond_to?(:encode) return self if utf8? self.encode('UTF-8',enc) else require 'iconv' if RUBY_VERSION.to_f < 1.9 return self if utf8? begin Iconv.iconv('UTF-8',enc,self).at(0).to_s rescue self end end end |
#utf8? ⇒ Boolean
判断是否为utf8
5 6 7 8 9 10 11 12 13 |
# File 'lib/vmopt/ext/string_ext.rb', line 5 def utf8? if String.method_defined?(:encode) return true if self.force_encoding("UTF-8").valid_encoding? return false else unpack('U*') rescue return false return true end end |