performance - Can retrieve attributes of elements in a hash in Ruby, but not the elements themselves -


i'm having huge problems retrieving elements hash node_hash in code below. hash contains nodes of phylogenetic tree, built data available here. each node has unique id, key of hash. when try access element of hash e.g., node_hash[10], ruby spins wheels , cannot retrieve it. however, if node_hash[10].name name returned. know proper relationships being set because can

node_hash[1].children.each |child|   puts child.name end 

and expected output of

root viruses viroids unclassified sequences other sequences cellular organisms 

but waits after printing that. here full code.

class node   attr_accessor :name, :kind, :children, :parent   def initialize(name=nil, kind=nil, children=nil, parent=nil)     @name   = name     @kind   = kind     @parent = parent     if children       @children = children     else       @children = []     end   end   def add_child(child)     @children << child   end end  node_hash = {}  file.open("taxdump/names.dmp", "r") |name_file|   name_file.each_line |line|     split_line = line.split("|")     if split_line[3].include? 'scientific name'       id_num = integer(split_line[0])       name   = split_line[1].strip       node_hash[id_num] = node.new(name)     end   end end  file.open("taxdump/nodes.dmp", "r") |node_file|   node_file.each_line |line|     split_line = line.split("|")     id_num = integer(split_line[0].strip)     par_id = integer(split_line[1].strip)]     child  = node_hash[id_num]     parent = node_hash[par_id]     child.parent = parent     child.kind   = split_line[2].strip     parent.add_child(child)   end end 

edit: let 1 of nodes load, , after minute, looks entire structure of tree printed. seems ruby recursively walking tree (displaying node's parent , children, parent's parent , children, etc. i'm going try , override inspect. work.

edit 2: did trick.

so let 1 of nodes load, , after minute, looks entire structure of tree printed. ruby recursively walked tree (displaying node's parent , children, parent's parent , children, etc. generating string taking long. overriding inspect change how each node displayed fixed problem.


Comments

Popular posts from this blog

delphi - How to convert bitmaps to video? -

jasper reports - Fixed header in Excel using JasperReports -

python - ('The SQL contains 0 parameter markers, but 50 parameters were supplied', 'HY000') or TypeError: 'tuple' object is not callable -