开发者

Recursive DFS Ruby method

开发者 https://www.devze.com 2023-01-28 22:25 出处:网络
I have a YAML file of groups that I would like to get into a MongoDB collection called groups with documents like {\"name\" => \"golf\", \"parent\" => \"sports\"} (Top level groups, like sports,

I have a YAML file of groups that I would like to get into a MongoDB collection called groups with documents like {"name" => "golf", "parent" => "sports"} (Top level groups, like sports, would just be {"name" => "sports"} without a parent.)

We are trying to traverse the nest开发者_如何学Goed hash, but I'm not sure if it's working correctly. I'd prefer to use a recursive method than a lambda proc. What should we change to make it work?

Thanks!

Matt


Here's the working code:

require 'mongo'
require 'yaml'

conn = Mongo::Connection.new
db = conn.db("acani")
interests = db.collection("interests")
@@interest_id = 0
interests_hash = YAML::load_file('interests.yml')

def interests.insert_interest(interest, parent=nil)
  interest_id = @@interest_id.to_s(36)
  if interest.is_a? String # base case
    insert({:_id => interest_id, :n => interest, :p => parent})
    @@interest_id += 1
  else # it's a hash
    interest = interest.first # get key-value pair in hash
    interest_name = interest[0]
    insert({:_id => interest_id, :n => interest_name, :p => parent})
    @@interest_id += 1
    interest[1].each do |i|
      insert_interest(i, interest_name)
    end
  end
end

interests.insert_interest interests_hash

View the Interests YAML.
View the acani source.


Your question is just how to convert this code:

insert_enumerable = lambda {|obj, collection|
   # obj = {:value => obj} if !obj.kind_of? Enumerable
   if(obj.kind_of? Array or obj.kind_of? Hash)
      obj.each do |k, v|
        v = (v.nil?) ? k : v
        insert_enumerable.call({:value => v, :parent => obj}, collection)
      end
   else
      obj = {:value => obj}
   end
   # collection.insert({name => obj[:value], :parent => obj[:parent]})
   pp({name => obj[:value], :parent => obj[:parent]})
}

...to use a method rather than a lambda? If so, then:

def insert_enumerable( obj, collection )
   # obj = {:value => obj} if !obj.kind_of? Enumerable
   if(obj.kind_of? Array or obj.kind_of? Hash)
      obj.each do |k, v|
        v = (v.nil?) ? k : v
        insert_enumerable({:value => v, :parent => obj}, collection)
      end
   else
      obj = {:value => obj}
   end
   # collection.insert({name => obj[:value], :parent => obj[:parent]})
   pp({name => obj[:value], :parent => obj[:parent]})
end

If that's not what you're asking, please help clarify.

0

精彩评论

暂无评论...
验证码 换一张
取 消