ruby 散列表 报 can't convert Hash into Integer

问题描述:

ruby 散列表 报 can't convert Hash into Integer
我想建立一个散列表,但是不会用.
irb(main):011:0> hashtest=['a'=>'c','b'=>'d','c'=>'e','d'=>'f']
=> [{"a"=>"c","b"=>"d","c"=>"e","d"=>"f"}]
irb(main):012:0> hashtest['a']
TypeError:can't convert String into Integer
\x05from (irb):12:in `[]'
\x05from (irb):12
\x05from :0
并且irb(main):013:0> hashtest.size
=> 1
为啥是1呢?
irb(main):014:0> hashtest.empty?
=> false
irb(main):015:0> hashtest.keys
NoMethodError:undefined method `keys' for [{"a"=>"c","b"=>"d","c"=>"e","d"=>"f"}]:Array
\x05from (irb):15
\x05from :0
irb(main):016:0> hashtest.values
NoMethodError:undefined method `values' for [{"a"=>"c","b"=>"d","c"=>"e","d"=>"f"}]:Array
\x05from (irb):16
\x05from :0
这是为什么.

你写的不是散列表 是数组
hashtest={'a'=>'c','b'=>'d','c'=>'e','d'=>'f'}才对