1

我有一个存储在变量 temp 中的数组,如下所示:

temp.inspect 输出:

[#"Marlana Letelier", "completed_at"=>nil, "status"=>"incomplete", "name"=>nil, "lead_move_date"=>"2012-06-17 00:00:00", "archive_time "=>nil, "stop_time"=>nil, "priority"=>"2", "assigned_to_user_firstname"=>"Vanessa", "notes"=>"", "created_by_id"=>nil, "id"=> “804005”、“assigned_to_id”=>“1”、“dn_email_id”=>nil、“outcomes_string”=>“其他”、“lead_id”=>“101139”、“flavor”=>“PhonecallTask​​”、“stringified_pa​​rameters” =>"{\n'目的' => '继续联系尝试',\n'电话号码' => '361-946-9905',\n}", "created_at"=>"2011-12-21 13:29:07", "start_time"=>"2012-04-04 17:00:00"}>]

temp.class 将其指定为数组,但 temp[1] 不输出任何内容。

如何访问元素?

编辑:
1) Temp 要么一无所有,1 个对象或多个对象
2) 检查 nil
3) 取出每个对象
4) 访问属性

4

1 回答 1

0

尽管您的检查输出看起来错误(我认为您缺少一些出现在<...>标签中的文本),但看起来您有一个包含单个项目的数组。通过输出 temp.length 验证此假设。

由于 Ruby 中的数组是 0 索引的,请尝试使用temp[0]temp.first.

于 2012-04-05T17:12:52.150 回答