A
Alex Stahl
Recently, I needed to write some code to traverse friend graphs on my
website. Due to my C++ background, I used recursion:
def traverse(user, depth, origin=nil)
@hi5 = Hi5fbapi.new @api_key, @secret
if depth > 0
friends = @hi5.get_friends(user)
friends.delete origin if friends.include? origin
depth -= 1
friends.each do |friend|
traverse(friend, depth, user)
end
end
end
But as I continue to learn the finer points of ruby, I can't help but
think there's a more ruby-oriented way to do this. Any suggestions?
Thanks,
Alex
website. Due to my C++ background, I used recursion:
def traverse(user, depth, origin=nil)
@hi5 = Hi5fbapi.new @api_key, @secret
if depth > 0
friends = @hi5.get_friends(user)
friends.delete origin if friends.include? origin
depth -= 1
friends.each do |friend|
traverse(friend, depth, user)
end
end
end
But as I continue to learn the finer points of ruby, I can't help but
think there's a more ruby-oriented way to do this. Any suggestions?
Thanks,
Alex