ruby - How do I return a value from inside a loop? -
i trying read xml file , store structure array of objects. here code:
class bike attr_accessor :id, :color, :name def initialize(id, color, name) @id = id @color = color @name = name end end ---x---snip---x--- rules.root.each_element |node1| case node1.name when "bike" bike = bike.new(node1.attributes['id'], node1.attributes['color'], { |bike_elem| bike_elem.text.chomp if bike_elem.name == "name"}) bikes.push bike end end
however, last element not fetching value alone. fetching whole tag. there better way it?
your code block { |bike_elem| bike_elem.text.chomp if bike_elem.name == "name" }
doesn't seem make sense inside new
parameter list.
where bike_elem
come from? not valid ruby in context.
it's hard give answer here without knowing xml looks like. recommend using xml library nokogiri, libxml, , parse out name before new
. try using xpath.
rules.root.each_element |node1| case node1.name when "bike" name = node1.attributes[....] bike = bike.new(node1.attributes['id'], node1.attributes['color'],name ) bikes.push bike end end
Comments
Post a Comment