【问题标题】:Accessing attributes of Struct contained in Struct访问 Struct 中包含的 Struct 的属性
【发布时间】:2013-12-14 11:15:20
【问题描述】:

我可能犯了一个重大的设计错误,但这是我第一次在 Ruby 中使用 Structs。

As detailed in this question,我有三个对象,Vertex、Edge 和 Graph。 Vertex 有简单的属性(标量),但 Edge 可以有 :endpoints,它是数组中的一对顶点。具体来说,Edge 有一个标量 :weight 和一个顶点数组 :endpoints。最后,Graph 存储了:vertexes:edges,它们是顶点和边的数组。

Edge = Struct.new(:weight, :endpoints)

由于 Graph 是一个包含结构体的结构体,我创建了一个方法来从 Graph 中获取标量 weights

Graph = Struct.new(:vertexes, :edges) do
  def get_weights()
    w = []
    self.edges.each do |ed| w << ed.weight end
  end

#truncated
end

但是,如果我运行它,我会得到边,而不是标量整数:

[226] pry(main)> t_weights = [4,8,8,11,7,4,2,9,14,10,2,1,6,7]
=> [4, 8, 8, 11, 7, 4, 2, 9, 14, 10, 2, 1, 6, 7]

[227] pry(main)> t_edges.each_with_index.map do |ed,ind|
[227] pry(main)*   ed.weight = t_weights[ind]  
[227] pry(main)*   ed.endpoints = endpoints[ind]  
[227] pry(main)*   # p ed.weight  
[227] pry(main)* end  
t_grap = Graph.new(t_verts, t_edges)

=> #<struct Graph
 vertexes=
  [#<struct Vertex parent=nil, rank=nil, id=0.31572617312378737>,
   #<struct Vertex parent=nil, rank=nil, id=0.24063512558288636>,
   #<struct Vertex parent=nil, rank=nil, id=0.34820800389791284>,
   #<struct Vertex parent=nil, rank=nil, id=0.86247407897408>,
   #<struct Vertex parent=nil, rank=nil, id=0.4503814825928186>,
   #<struct Vertex parent=nil, rank=nil, id=0.4020451841058619>,
   #<struct Vertex parent=nil, rank=nil, id=0.09096934472128582>,
   #<struct Vertex parent=nil, rank=nil, id=0.9942198795853134>,
   #<struct Vertex parent=nil, rank=nil, id=0.4393226273344629>], <truncated>
 edges=
  [#<struct Edge
    weight=4,
    endpoints=
     [#<struct Vertex
       parent=#<struct Vertex:...>,
       rank=0, <truncated>

[230] pry(main)> t_grap.get_weights
=> [#<struct Edge
  weight=4,
  endpoints=
   [#<struct Vertex
     parent=#<struct Vertex:...>,
     rank=0,
     id=0.6540666040368713>,
    #<struct Vertex
     parent=#<struct Vertex:...>,
     rank=0,
     id=0.7511069577638254>]>,
 #<struct Edge
  weight=8,
  endpoints=
   [#<struct Vertex
     parent=#<struct Vertex:...>,
     rank=0,
     id=0.6540666040368713>,<truncated>

我尝试将 attr_accessor :weights 添加到 Edge,但是上面设置初始值的代码 sn-p 会静默失败,使每个 Edge.weight 等于 nil

我做错了什么?

【问题讨论】:

    标签: ruby struct


    【解决方案1】:

    不确定为什么在调用 Graph.new 时需要顶点

    边缘应该已经在端点中包含它们。

    Vertex = Struct.new(:x, :y)
    Edge = Struct.new(:weight, :endpoints)
    
    Graph = Struct.new(:edges) do
      def get_weights
        edges.map(&:weight)
      end
    end
    
    e1 = Edge.new 66, [Vertex.new(1,2), Vertex.new(2,1)]
    e2 = Edge.new 26, [Vertex.new(1,1), Vertex.new(2,1)]
    g = Graph.new([e1,e2])
    g.get_weights
    => [66, 26] 
    

    编辑:

    g.edges.each { |edge| p edge.weight }
    66
    26
    ...
    

    有效。您可能正在构建不同的东西。上面的代码创建了一个 Graph 对象,其中包含一个称为边的 Edge 对象数组。

    ed_ary.map[&:weight]
    

    行不通。正如我的评论g.edges.map(&amp;:weight) 只是一个捷径。请参阅此讨论:What does to_proc method mean?


    【讨论】:

    • 绝对有效,但我无法理解放大器操作员。如果我在 Graph 之外只有一个边数组,为什么 ed_ary.each do |ed| p ed.weight ended_ary.map[&amp;:weight] 不返回存储的权重?
    • 对不起,我应该保持熟悉。同edges.map { |edge| edge.weight }
    猜你喜欢
    • 2012-03-08
    • 2013-09-24
    • 2021-07-22
    • 1970-01-01
    • 2020-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-14
    相关资源
    最近更新 更多