ruby入门
2009-12-18ruby入门
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'