2012-04-05 143 views
1

我具有存储在一个变量temp,看起来像这样的数组:Ruby On Rails:访问数组?

temp.inspect输出:

[# “Marlana莱特列尔”, “completed_at”=>零, “状态”=>“不完整的“,”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”=> results_string“=>”other“,”lead_id“=>”101139“,”flavor“=>”PhonecallTask​​“,”stringified_pa​​rameters“=>”{\ n'purpose'=>'继续联系尝试',\ n'phone_number '=>'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)温度要么什么都没有,1个或多个对象
2)检查零
3)获取每个对象了
4)访问属性

回答

0

虽然你检查输出看起来不对(我想你错过了<...>标签中出现的一些文本),看起来你有一个单一项目的数组。通过输出temp.length来验证这个假设。

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

+0

Temp.length是1。那么我们如何去提取这些数据呢? – sunny31 2012-04-05 17:17:13

+0

@ sunny31正确;因此'temp [1]'试图访问一个不存在的元素。你想要'temp [0]'或'temp.first'。 – Phrogz 2012-04-05 17:18:11

+0

这只是吐出#而没有别的。试过temp [0]和temp.first – sunny31 2012-04-05 17:19:30