Skip to main content
1 of 2
Flambino
  • 33k
  • 2
  • 45
  • 90

Start by using map instead of each plus <<; creating an array and then adding to it isn't very Ruby-like.

Second, Node#attributes is a hash already, so one way to go is to modify a duplicate of it, rather than "manually" copying each key/value to a new hash. Also, the keys are strings so to_s.to_sym can be replaced by just to_sym.

To do the hash-conversion, I've used the same keys.each approach as what Rails uses in its symbolize_keys! method.

def get_training_queue(xml_data)
  xml_data.xpath("//row").map do |row|
    skill = row.attributes.dup
    skill.keys.each do |key|
      skill[key.to_sym] = skill.delete(key).to_s
    end
    skill
  end
end

Here's a different, super-brief, approach that uses the Hash[ [key, value] , ... ] syntax

def get_training_queue(xml_data)
  xml_data.xpath("//row").map do |row|
    Hash[ row.attributes.to_a.map { |k, v| [k.to_sym, v.to_s] } ]
  end
end

Either one should give you the right result, though.

Flambino
  • 33k
  • 2
  • 45
  • 90