0

メソッドでフォロワーの数のみを返すようにします。最後の「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

ハッシュを含む social_profiles 配列内に Twitter プロファイルが存在する場合、フォロワーを返すようにしたい (したがって、次の ["followers"]

[{"url"=>" http://gravatar.com/xxxxxx ", "id"=>"xxxxxx", "type"=>"gravatar", "type_id"=>"gravatar", "type_name"= >"Gravatar", "username"=>"xxxxxxx", "bio"=>"SOME LARGE BIO"}, {"url"=>" http://www.facebook.com/xxxxxxx ", "type"= >"facebook", "type_id"=>"facebook", "type_name"=>"Facebook", "ユーザー名"=>"xxxxxx", "id"=>"xxxxxx"}, {"url"=>" http ://www.quora.com/xxxxxxx "、"タイプ"=>"quora"、"ユーザー名"=>"xxxxxx"、"type_id"=>"quora", "type_name"=>"Quora"}, {"url"=>" http://twitter.com/xxxxxxx ", "type"=>"twitter", "username"=> "xxxxx", "type_id"=>"twitter", "フォロワー" => "1000" "type_name"=>"Twitter"}, {"url"=>" http://plancast.com/user/xxxxxx=>" http://plancast.com/user/xxxxxx=>" http://plancast.com/user/xxxxxx", "id"=>"xxxxxx", "type"=>"plancast", "type_id"=>"plancast", "type_name"=>"Plancast"}, {"url"=>" http:// youtube.com/user/xxxxxxx ", "type"=>"youtube", "username"=>"xxxxxx", "type_id"=>"youtube", "type_name"=>"Youtube"}]

ありがとう!

4

1 に答える 1

0

これはうまくいくはずだと思います。あなたが何を求めているのか、私が実際に理解しているなら。

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 (ゼロ) が返されます。

于 2013-09-11T05:28:46.440 に答える