我有一个数组:
row = [:text_field,
[{:text=>
"I<NLDSPECI20142999999990<<<<<86503101F2403096NLD<<<<<<<<<<<8DE<BRUIJN<<WILLEKE<LISELOTTE<<",
:value=>"999999990",
:@derived=>"false",
:@id=>"_7a88b4df-8730-46d9-9a13-6494940fcfce-fg-1-fd-1",
:@name=>"CitizenServiceNumber",
:@score=>"1"},
{:text=>
"I<NLDSPECI20142999999990<<<<<86503101F2403096NLD<<<<<<<<<<<8DE<BRUIJN<<WILLEKE<LISELOTTE<<",
:value=>"NLD",
:@derived=>"false",
:@id=>"_7a88b4df-8730-46d9-9a13-6494940fcfce-fg-1-fd-2",
:@name=>"CountryCode",
:@score=>"1"}]]
我打电话的时候:
row.each {|row_info| row_info[:@name]}
我收到错误消息:
TypeError (no implicit conversion of Symbol into Integer)
我在这里做错了什么?
答案 0 :(得分:3)
:text_field
是Symbol
类的实例。它不响应[]
方法。
以下不会抛出任何异常,因为它什么也不做:
row.last.each {|row_info| row_info[:@name]}
答案 1 :(得分:3)
您希望迭代的集合位于row[1]
而不是row
row[1].map { |ri| ri[:@name] }
#=> ["CitizenServiceNumber", "CountryCode", "DocumentNumber", "DocumentType", "Gender", "GivenNames", "Initials", "Nationality", "Prefixes", "Surname"]