我有很多哈希
[{"id"=>1,
"name"=>"Bose Headphones",
"created_at"=>"2015-11-25T10:40:29.120Z",
"updated_at"=>"2015-11-25T10:40:29.120Z",
"description"=>"Bose",
"active"=>true},
{"id"=>3,
"name"=>"test topic",
"created_at"=>"2015-11-30T14:34:03.087Z",
"updated_at"=>"2015-11-30T14:34:03.087Z",
"description"=>"test",
"active"=>true},
{"id"=>4,
"name"=>"Wireless Mouse",
"created_at"=>"2015-11-30T14:35:16.583Z",
"updated_at"=>"2015-11-30T14:35:16.583Z",
"description"=>"WM",
"active"=>true},
{"id"=>5,
"name"=>"Fit Band",
"created_at"=>"2015-12-01T04:39:03.034Z",
"updated_at"=>"2015-12-01T04:39:03.034Z",
"description"=>"Fitness Band",
"active"=>true}]
我想获取数组中的所有名称值,然后用于.sample
从列表中获取所需的任何数字。我正在尝试的方式是-
arr = []
arr = arrOfHash.map{|x| "#{x['name']}"}.sample(1)
这给我
如果不存在“多个”属性,则值不能为数组。不是数组(ArgumentError)
我已经对此进行了测试,它似乎按预期工作!
arrOfHashes.map{|i| i["name"]}.sample(1)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句