几个有用的方法

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]

你可能感兴趣的:(PHP,Blog)