Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 在Ruby中创建要放入数组的新类_Ruby On Rails_Ruby_Arrays - Fatal编程技术网

Ruby on rails 在Ruby中创建要放入数组的新类

Ruby on rails 在Ruby中创建要放入数组的新类,ruby-on-rails,ruby,arrays,Ruby On Rails,Ruby,Arrays,我来自C#背景,试图学习Ruby和RubyonRails。我有以下Car类-请注意我需要的build_xml方法,以便以该语法构建xml,然后传递给Web服务 class Car @@array = Array.new #this will allow us to get list of all instances of cars created if needed def self.all_instances @@array end def initialize

我来自C#背景,试图学习Ruby和RubyonRails。我有以下Car类-请注意我需要的build_xml方法,以便以该语法构建xml,然后传递给Web服务

class Car

  @@array = Array.new
  #this will allow us to get list of all instances of cars created if needed
  def self.all_instances
    @@array
  end

  def initialize(id, model_number, engine_size, no_doors)
    # Instance variables
    @id = id
    @model_number = model_number
    @engine_size = engine_size
    @no_doors = no_doors
    @@array << self
  end

  def build_car_xml
    car = { 'abc:Id'=> @id, 'abc:ModelNo' => @model_number, 'abc:ES' => @engine_size, 'abc:ND' => @no_doors}
   cars = {'abc:Car' => [car] }   
  end

end
这与预期的一样,请求的格式是我所需要的,Web服务返回结果。不过,我现在想扩展它,这样我就可以传入一系列汽车并生成请求XML——不过我很难让这部分正常工作

到目前为止,我一直在尝试以下操作(目前我只需要更改Id即可,因为它是唯一需要的参数):

我不确定的是:1)这是Ruby中正确的操作方法吗?2)如何构造方法,使其以正确的格式构建XML,就像我在单个对象上调用它时的格式一样,也就是说,我需要在实际值之前添加名称空间

def build_car_xml(car_array)
 #here is where I am unsure how to contruct this method   
end
可能的解决方案(
'abc:Car'
是一个错误的名称,如果您希望它包含一个数组,应该是
Cars
):


它不符合您的要求,因为实例
build\u car\u xml
不会生成
car
名称空间,但对我来说这是不一致的。XML实际上是一个集合,即使它只有一个元素,实例方法也不应该负责集合
Car.build\u cars\u xml([Car.new(…)])
在我看来更符合逻辑。

可能很快就会出现这样一种情况:使用
Car
类作为应用于“汽车集合”的方法的占位符是站不住脚的,您将希望正确地具体化该集合。如果“汽车集合”具有Ruby
数组中尚未包含的属性(例如XML开始和结束元素),然后创建一个新类。这感觉不应该用ruby on rails标记它。rails有很多基础设施,可以通过数据库表组织对象集合,而您似乎在这里滚动自己的组织。我也同意Neil的观点,您不应该使用Car类来保存我当前的所有Car对象实例mory,我觉得这是不对的。我会有另一个类来处理这个问题,它有一个实例变量,包含一个Car对象数组。@mbrach-你能不能用一个答案来扩展一下,如何在数组中的每个对象上按原样调用build_Car_xml-以及如何构建它?Victor-这对我很有用-我正在学习Ruby-你能解释一下吗cars.map(&:build_car_xml)符号正在做什么?
cars.map{x | x.build_car_xml}
  car_array = []
  (1..10).each do |i|
    car_array << Car.new(i.to_s, 18, 3.0, 4)
  end
request = Car.build_car_xml(car_array)
def build_car_xml(car_array)
 #here is where I am unsure how to contruct this method   
end
class Car

...

  def self.build_cars_xml(cars)
    { 'abc:Car' => cars.map(&:build_car_xml) }   
  end

  def build_car_xml
    { 'abc:Id'=> @id, 'abc:ModelNo' => @model_number, 'abc:ES' => @engine_size, 'abc:ND' => @no_doors }
  end
end

cars =
  (1..10).map do |i|
    Car.new(i.to_s, 18, 3.0, 4)
  end
Car.build_cars_xml(cars)