ע | ̳

ChinaUnix.net

  ƽ̨ ̳ ֤ר IT Ƶ Ŀ ɳ Բ Ƶ Զά C/C++ PHP MySQL Ƕʽ Linuxϵͳ
̳ Ruby rubyɨä֮Hashʮһ
ʰ
鿴: 32870 | ظ: 8

rubyɨä֮Hashʮһ []

̳:
0
2010-12-02 18:03 |ʾȫ¥
zuerrong 2010-12-02 18:27 ༭

1. δHash?

x = Hash.new
x = {}
x = {:a => 1, :b => 2}


ЩǴHashķ
һ͵ڶһʾյHash.
ʾHash2Լ/ֵ

2. HashļЩ?

κĿ꣬磺

irb(main):002:0> x={}
=> {}
irb(main):003:0> x[1]=2
=> 2
irb(main):004:0> x["a"]="b"
=> "b"
irb(main):005:0> x[:z] = 100
=> 100
irb(main):006:0> x[[1,2,3]] = [4,5,6]
=> [4, 5, 6]
irb(main):007:0> x
=> {"a"=>"b", [1, 2, 3]=>[4, 5, 6], 1=>2, :z=>100}


ע⣺
1κĿ顢HashKey
2ʹַΪKeyʱʹöӦķš磬"a":a档Ϊÿַ"a"һĿ꣬:aΨһģ

ʡڴ档

3. θHashĬϵֵ?

irb(main):003:0> x=Hash.new([])
=> {}
irb(main):004:0> x[:a]
=> []


ʾһHashĬֵǿյ[]
Ȼx[:a]Ȼûȶ壬Ĭֵ

ע⣺ַĬֵָͬһĿ꣬ĿıˣӰ쵽Ĭֵ

irb(main):005:0> x[:a] << 1
=> [1]
irb(main):006:0> x[:b]
=> [1]


ҪÿӦΨһĬֵӦ˴Hash:

irb(main):007:0> y=Hash.new do |h,k| h[k] =[] end
=> {}


ٿ

irb(main):009:0> y[:a] << 1
=> [1]
irb(main):010:0> y[:b]
=> []


һֵĸı䣬Ӱ쵽Ĭֵ

4. αHash?

irb(main):018:0* x={1=>2,3=>4,5=>6}                  
=> {5=>6, 1=>2, 3=>4}

irb(main):019:0> x.each do |k,v| puts "#{k} #{v}" end   
5 6
1 2
3 4
=> {5=>6, 1=>2, 3=>4}


ʹHasheach

5. αHashKeyValue?

irb(main):020:0> x.keys.each do |k| puts k end

irb(main):021:0> x.values.each do |v| puts v end


ʹHashkeysvaluesǷһ顣

Ȼʹeach_keyeach_value˼DZkeyvalue.

irb(main):016:0> x.each_key do |s| puts s end
1
3
=> {1=>2, 3=>4}
irb(main):017:0> x.each_value do |s| puts s end   
2
4
=> {1=>2, 3=>4}


6. Hashsort?

rubyhashʵǿsortģһsortĶά顣

irb(main):027:0> x
=> {5=>6, 1=>2, 3=>4}

irb(main):028:0> x.sort
=> [[1, 2], [3, 4], [5, 6]]


rubyOrderedHash࣬HashԪذ˳sort

7. δHashӺɾԪ?

ԪأֱӶKey/Value

irb(main):040:0> x[7]=8
=> 8

irb(main):041:0> x
=> {5=>6, 1=>2, 7=>8, 3=>4}


ɾԪأʹdeleteɾKey:

irb(main):042:0> x.delete(7)
=> 8

irb(main):043:0> x
=> {5=>6, 1=>2, 3=>4}


ȻҲdelete_ifɾHashԪءɾKey3ģ

irb(main):074:0* x.delete_if do |k,v| k>3 end
=> {1=>2, 3=>4}


8. βHashԪ?

ʹhas_key?has_value?жǷжӦֵ

irb(main):052:0> x.has_key? 1
=> true

irb(main):053:0> x.has_value? 4
=> true

irb(main):054:0> x.has_key? 9
=> false


ǵĬֱֵif hash[:key]жϣ磺

irb(main):055:0> y=Hash.new(9)
=> {}

irb(main):056:0> y.has_key? :a
=> false

irb(main):057:0> puts 1 if y[:a]
1
=> nil


IJҷfindselectȣҵKey2Ԫأ

irb(main):038:0> x.select do |k,v| k>2 end
=> [[5, 6], [3, 4]]


9. ԷתHash?

ǿԣʹinvert

irb(main):011:0* x={1=>2,3=>4,5=>6}
=> {5=>6, 1=>2, 3=>4}

irb(main):012:0> x.invert
=> {6=>5, 2=>1, 4=>3}


10. Hashת?

Hashתܼ򵥣

irb(main):020:0> x.to_a
=> [[5, 6], [1, 2], [3, 4]]


תHash鷳һ㣬Լдһto_hash

irb(main):023:0> class Array
irb(main):024:1>   def to_hash
irb(main):025:2>     unless size%2 == 0
irb(main):026:3>       raise "array with odd number of elements"
irb(main):027:3>     end
irb(main):028:2>     hash = Hash.new
irb(main):029:2>     0.step(size-1,2) {|x| hash[self[x]] = self[x+1] }
irb(main):030:2>     hash
irb(main):031:2>   end
irb(main):032:1> end
=> nil

irb(main):033:0> x=[1,2,3,4,5,6]
=> [1, 2, 3, 4, 5, 6]

irb(main):034:0> x.to_hash
=> {5=>6, 1=>2, 3=>4}


11. HashĶֵΪ?

κʱ޸HashKeyסKeyǰڵĿֵ꣩ᷢΪ

irb(main):001:0> h=Hash.new
=> {}
irb(main):002:0> x=[1,2,3]
=> [1, 2, 3]
irb(main):003:0> h[x]=4
=> 4
irb(main):004:0> h[x]
=> 4
irb(main):005:0> x<<4
=> [1, 2, 3, 4]
irb(main):006:0> h[x]


޸˼xֵôӦValueͶʧˡ
취rehashκʱ޸KeyֵҪrehashһΡ

irb(main):007:0> h.rehash
=> {[1, 2, 3, 4]=>4}
irb(main):008:0> h[x]   
=> 4

̳:
0
2010-12-02 23:11 |ʾȫ¥
{:3_200:}֧¥

Ա

̳:
49
15-16CBA֮
:2016-06-22 16:22:002015ޱ֮й
:2015-01-23 16:25:12ţ
:2015-01-20 09:39:23δ
:2015-01-14 23:55:57
:2015-01-06 18:21:36˫
:2015-01-02 22:04:33
:2014-11-25 09:58:35
:2014-11-18 10:40:07
:2014-11-13 22:47:15
:2014-10-22 15:29:50Ħ
:2014-08-27 10:49:43ChinaUnixԪ
:2015-01-30 15:14:07
2010-12-05 20:17 |ʾȫ¥
֧¥λǺ

̳:
0
2010-12-05 22:05 |ʾȫ¥
һɡ

̳:
0
2011-02-22 19:05 |ʾȫ¥
irb(main):003:0> x=Hash.new([])
=> {}
irb(main):004:0> x[:a]
=> []


ûס
1.[a]ôģ
2.aǰΪɶи    

̳:
0
2011-09-08 12:20 |ʾȫ¥
ظ 5# ziyiu123

:a rubyеķ(symbol)
Բοhttp://www.iteye.com/topic/109697

л

̳:
0
2011-11-17 23:05 |ʾȫ¥
ס

̳:
7
繷
:2013-12-15 20:43:38ͼ
:2014-03-05 01:33:12ͼ
:2014-03-15 20:31:17δ
:2014-03-25 23:48:20ţ
:2014-04-07 22:37:44
:2014-04-11 21:58:0915-16CBA֮ൺ
:2016-03-17 20:36:13
2013-02-05 18:58 |ʾȫ¥
ٿ

̳:
4
ݿ⼼ÿշ֮
:2015-11-27 06:20:00ưÿշ֮
:2015-12-01 06:20:00ÿ̳֮
:2015-12-01 06:20:0015-16CBA֮ɽ
:2017-03-26 23:38:03
2016-01-06 15:30 |ʾȫ¥
˸о


Ŀǰ
б

ֹ

  

𩳽Ϣ޹˾. Ȩ ICP֤:060528 йֺ־ıţ1101082001
㲥ӽĿӪ֤() ֵ1234 йЭԱ  ϵǣ
лйĺֹ֧ChinaUnix תرվעԭ

Cookies - ChinaUnix - Archiver - WAP - TOP