saberma

分享技术实践,创业历程

ruby入门

2009-12-18

ruby入门

Ruby API查询
Ruby 正则表达式查询

ruby技巧

字符串转数组

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

数组转为哈希

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

char的ASCII值

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

字符编码转换

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

字符串转换为类

Kernel.const_get 'Integer'

Iconv

blog comments powered by Disqus
Fork me on GitHub