ruby入门

2009-12-18

h2. ruby入门

“Ruby API查询”:http://apidock.com/ruby “Ruby 正则表达式查询”:http://www.rubular.com

h2. ruby技巧

h3. 字符串转数组

s = "a=1&b=2"
a = s.split(/&|=/)
#["a", "1", "b", "2"]

h3. 数组转为哈希

a = ["a", "1", "b", "2"]
#*号可以将数组中的字符串展开
Hash[*a]
#{"a"=>"1", "b"=>"2"}

h3. char的ASCII值

#ASCII值转为char
120.chr
#char转为ASCII值
'x'.ord
#查看字符串的ASCII值
"love".chars.map(&:ord)

h3. 字符编码转换

require 'iconv'
#conv(to, from, str)
Iconv.conv('gb2312', 'utf-8', '中国')

h3. 字符串转换为类

Kernel.const_get 'Integer'

“Iconv”:http://www.kuqin.com/rubycndocument/man/addlib/Iconv.html

blog comments powered by Disqus