我想读取一个XML文件并将结构存储到一个对象数组中。这里是我的代码:如何从循环内部返回值?
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 do |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
但是,最后一个元素没有单独获取值。它正在读取整个标签。有没有更好的方法来做到这一点?
我不认为这是一个有效的Ruby表达式。 – sawa 2013-05-14 16:50:42
我不认为你的问题与迭代(或者说,你的话中的“循环”)有任何关系。 – sawa 2013-05-14 16:53:00