我要我的方法只返回追随者的数量。我发现,如果它没有进展到最后的' if‘语句,它只返回前面代码中的查询或散列。
def my_twitter_followers
if JSON.parse(contact_hash)["social_profiles"]
JSON.parse(contact_hash)["social_profiles"].each do |profile|
if profile["followers"]
return profile["followers"]
end
end
end
end如果Twitter配置文件存在于包含散列的social_profiles数组中,我希望它返回关注者(这就是为什么下面的“追随者”)
[{"url"=>"http://gravatar.com/xxxxxx“、”id“”=>“xxxxxx”、“键入”=>“Gravatar”、"type_id"=>"gravatar“、"type_name"=>"Gravatar”、“用户名”“=>”xxxxxx“、”BIO“=>”一些大型生物“}、{”url“”=>“http://www.facebook.com/xxxxxxx、”type“”Facebook“、"type_id"=>"facebook”、"type_name"=>"Facebook“、"username"=>"xxxxxx",“id”=>“xxxxx”}、{"url"=>"http://www.quora.com/xxxxxxx“、”键入“=>”Quora“、”用户名“”=>“xxxxx、"type_id"=>"quora”、"type_name"=>"Quora"}、{"url"=>"http://twitter.com/xxxxxxx“、”键入“=>”Twitter“、”用户名“”=>“xxxxx、"type_id"=>"twitter”、“=> "1000”"type_name"=>"Twitter"}{"url"=>"http://plancast.com/user/xxxxxx“、”id“”=>“xxxxxx、”=>“Plancast、"type_id"=>"plancast”、"type_name"=>"Plancast"}、{"url"=>"http://youtube.com/user/xxxxxxx“、”键入“=>”Youtube“、”用户名“”=>“xxxxxx、"type_id"=>"youtube”、"type_name"=>"Youtube"}]
谢谢!
发布于 2013-09-11 05:28:46
我觉得这个应该管用。如果我真的明白你想要什么。
def my_twitter_followers
if JSON.parse(contact_hash)["social_profiles"]
JSON.parse(contact_hash)["social_profiles"].each do |profile|
if profile["type"] == "twitter"
return profile["followers"] unless profile["followers"].blank?
end
end
end
return 0
end在这种情况下,如果有一个twitter配置文件。它将返回推特追随者,除非没有追随者。在这种情况下,它将返回0(零)
https://stackoverflow.com/questions/18732818
复制相似问题