Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从Hashie::Mash Loop Ruby on Rails获取随机结果并限制输出_Ruby_Json_Api_Instagram - Fatal编程技术网

从Hashie::Mash Loop Ruby on Rails获取随机结果并限制输出

从Hashie::Mash Loop Ruby on Rails获取随机结果并限制输出,ruby,json,api,instagram,Ruby,Json,Api,Instagram,我正在使用Instagram API获取用户的照片提要。输出如下所示: [#<Hashie::Mash attribution=nil caption=nil comments=#<Hashie::Mash count=0 data=[]> created_time="1330231732" filter="Sutro" id="1403234234201396589_3002382" images=#<Hashie::Mash low_resolution=#<

我正在使用Instagram API获取用户的照片提要。输出如下所示:

 [#<Hashie::Mash attribution=nil caption=nil comments=#<Hashie::Mash count=0 data=[]> created_time="1330231732" filter="Sutro" id="1403234234201396589_3002382" images=#<Hashie::Mash low_resolution=#<Hashie::Mash height=306 url="http://distilleryimage5.s3.amazonaws.com/8fd08dfsdfsdf111e180d51231380fcd7e_6.jpg" width=306>
如何获得随机结果并限制仅显示一幅图像?我尝试过使用limit(x),但这会抛出一个错误。我只是想随机显示一个图像,而不是整个流

media = Instagram.user_recent_media(@client.user.id)
puts media.class
给出:

Array
知道它是一个数组后,您可以使用
sample
方法轻松获得一个随机元素:

random = media.sample
puts "#{random.images.low_resolution.url}"

请注意,
sample
方法在Ruby 1.8.7中命名为
choice

WOOOPW!谢谢,我都不知道。干杯s
random = media.sample
puts "#{random.images.low_resolution.url}"