首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

几个有用的步骤

2012-10-25 
几个有用的方法hash.invertReturns a new hash created by using hsh’s values as keys, and the keys as

几个有用的方法
hash.invert
Returns a new hash created by using hsh’s values as keys, and the keys as values.

h = { "n" => 100, "m" => 100, "y" => 300, "d" => 200, "a" => 0 }
h.invert   #=> {0=>"a", 100=>"n", 200=>"d", 300=>"y"}


使用的实例参考:http://railser.cn/index.php/blog/hash-invert

string.match
string = "my phone number is (123) 555-1234"
phone_re = /\((\d{3})\)\s+(\d{3})-(\d{4})/
m = phone_re.match(string)
p m.string
p m.inspect
p m.captures
p m[0]
p m[1]
p m[2]
p m[3]
p m[4]

热点排行