在Ruby中向数组添加对象的问题

时间:2015-04-11 23:01:27

标签: ruby arrays object

所以我对Ruby有这个奇怪的问题。我创建了一个名为Path的类,其中包含start_nodeend_node。然后我创建了一个名为paths的空数组,并添加了Path个对象。当我执行puts paths时,它会打印起始节点和结束节点的列表。但是当我尝试打印每个Path时,它只打印出地址(看起来像),而不是打印开始和结束节点。我未被允许访问start_node内每条路径的end_nodepaths。这是我的代码的基本结构:

path = Path.new(start_node, end_node)
paths.push(path) 
...
puts paths.to_s # prints list of paths and each path consists of start_node and end_node
# below lines of code prints the addresses
paths.each do |path|
    puts path.to_s
end

我们是否允许创建对象并将它们添加到Ruby中的数组中?如果是这样,我在这里做错了什么。提前感谢您的帮助。

以下是整个脚本:

class Path
    def initialize (start_node, end_node, color, type)
        @start_node = start_node #start node of path
        @end_node = end_node #end node of path
        @color = color#path color (Red, Blue, Green)
        @type = type # path type (Horse, Cable, Trolley, Bus)
    end
    def start_node
        @start_node
    end
    def end_node
        @end_node
    end
    def color
        @color
    end
    def type
        @type
    end
end

class Graph
    def initialize
        @paths = []
        @num_of_nodes
        @num_of_edges
    end
    def read_file(filename)
        File.foreach(filename).with_index do |line, line_num|
            array = []
            array = line.split
            if line_num == 0
                @num_of_nodes = array[0]
                @num_of_edges = array[1]
                puts @num_of_edges + " " + @num_of_nodes
            else
                start_node = array[0]
                end_node = array[1]
                color = array[2]
                type = array[3]
                path = Path.new(start_node, end_node, color, type)
                @paths << path
            end
        end
    end
    def paths
        @paths
    end 
    def print_paths
        @paths.each do |path|
            puts path.start_node.to_s
        end
    end
end

graph = Graph.new
graph.read_file("./grandpaTransitInput.txt")
graph.paths.each do |path|
    puts path.to_s
end

2 个答案:

答案 0 :(得分:0)

这对我来说很好。查看您的体验不同之处:

2.2.1 :014 > class Path
2.2.1 :015?>   attr_accessor :start_node, :end_node
2.2.1 :016?>   def initialize(s, e)
2.2.1 :017?>     @start_node = s
2.2.1 :018?>     @end_node = e
2.2.1 :019?>   end
2.2.1 :020?> end
 => :initialize 
2.2.1 :021 > p = Path.new(1, 2)
 => #<Path:0x007fd6fa058d80 @start_node=1, @end_node=2> 
2.2.1 :024 > paths = []
 => [] 
2.2.1 :025 > paths.push p
 => [#<Path:0x007fd6fa058d80 @start_node=1, @end_node=2>] 
2.2.1 :026 > paths.push p
 => [#<Path:0x007fd6fa058d80 @start_node=1, @end_node=2>, #<Path:0x007fd6fa058d80 @start_node=1, @end_node=2>] 
2.2.1 :029 > puts paths.to_s
[#<Path:0x007fd6fa058d80 @start_node=1, @end_node=2>, #<Path:0x007fd6fa058d80 @start_node=1, @end_node=2>]
=> nil 

答案 1 :(得分:0)

方法puts将在内部对给定对象调用to_s。您的类“Path”没有定义它应该如何转换为String。 Ruby将使用默认方法,它只打印一些地址。

但您可以添加自己的方法来进行对话,puts path将使用它

class Path
  ..
  def to_s
    "#{@type} path from #{@start_node} to #{@end_node} in #{@color}"
  end
end

数组是一种特殊情况,通常应该使用join进行打印。如果直接打印,ruby会在所有元素上调用inspect。这是为了帮助调试。这就是为什么你在数组上调用时会看到内部结构,而不是在单个对象上。