我正在尝试创建一个我可以在 Twitter 上直接发送消息的所有人的哈希数组。正确的数组看起来像[{id:12345,name:"john", profile_pic:"some_url"},{id:67890,name:"jim", profile_pic:"some_url"}]
我正在获取 all_followers 和 all_friends 并比较两者的内容。返回的每个对象都是一个哈希数组。我正在遍历一个数组并获取 ID,然后遍历第二个数组并检查哈希是否包含该 id 值。如果是这样,我会从原始哈希中获取一些详细信息,并将其发送到最终用于我的浏览器的较小哈希中。
执行此操作的代码是:
def get_direct_message_list(friend_list,follower_list)
names_and_pics = []
friend_list.each do |base|
follower_list.each do |compare|
if compare.has_value?(base["id"])
block_hash = {}
block_hash["id"] = base["id"]
block_hash["name"] = base["name"]
block_hash["profile_background_image_url"] = base["profile_background_image_url"]
names_and_pics << block_hash
end
end
end
names_and_pics
end
它基本上正在工作。我的测试套件如下所示,并且测试通过了。
context "get_direct_message_list" do
it "should take friends and followers and return name, pic and twitter_id in one list" do
followers = [{"id"=>1, "name" => "john", "profile_background_image_url" => "http://somewhere.com"}, {"id"=>2},{"id"=>3, "name" => "mike", "profile_background_image_url" => "http://somewhere.com"}]
friends = [{"id"=>1, "name" => "john", "profile_background_image_url" => "http://somewhere.com"},{"id"=>5, "name"=>"someoneelse", "profile_background_image_url"=> "http://somewhere"},{"id"=>4},{"id"=>3, "name" => "mike", "profile_background_image_url" => "http://somewhere.com"}]
get_direct_message_list(friends, followers).should == [{"id"=>1, "name" => "john", "profile_background_image_url" => "http://somewhere.com"},{"id"=>3, "name" => "mike", "profile_background_image_url" => "http://somewhere.com"}]
end
end
我的问题是返回的数组有一些奇怪的重复,即特别是 12 和 13 的 Twitter ID,bizstone 和 jack dorsey,这两个人都不关注我。它们每个都在最终阵列中被复制 3 次和 7 次。我不太确定要解决什么问题。我的第一个想法是,当.has_valuehashx.has_value?(13)
的 twitter id表示匹配是准确的时,类似的东西正在返回 true。134567' was being encountered but further experimentation with
我可以看什么?