重写OpenStruct的getter以便将其打印为哈希



GOAL:OpenStruct对象的值应该打印为哈希,而不是对象

可能的解决方案:覆盖OpenStruct类的getter

MyOpenStruct覆盖OpenStructnewto_h[]

class MyOpenStruct < OpenStruct
    def initialize(object=nil)
        @table = {}
        @hash_table = {}
        if object
            object.each do |k,v|
                if v.is_a?(Array)
                    other = Array.new()
                    v.each { |e| other.push(self.class.new(entry)) }
                    v = other
                end
                @table[k.to_sym] = (v.is_a?(Hash) ? self.class.new(v) : v)
                @hash_table[k.to_sym] = v
                new_ostruct_member(k)
            end
        end
    end
    def [](val)
        @hash_table[val.to_sym]
    end
end

但覆盖[]并没有任何区别。例如

irb(main):007:0> temp = MyOpenStruct.new({"name"=>"first", "place"=>{"animal"=>"thing"}})
=> #<MyOpenStruct name="first", place=#<MyOpenStruct animal="thing">>
irb(main):008:0> temp.name
=> "first"
irb(main):009:0> temp.place
=> #<MyOpenStruct animal="thing">
irb(main):010:0> temp["place"]
=> {"animal"=>"thing"}
irb(main):011:0> temp[:place]
=> {"animal"=>"thing"}
irb(main):012:0> temp
=> #<MyOpenStruct name="first", place=#<MyOpenStruct animal="thing">>

只有当我使用[]访问密钥时,才会返回哈希!!

我该如何更正??

我认为,如果您将嵌套的OpenStruct作为哈希返回,那么创建它是没有意义的。这就是OpenStruct的工作方式:

require 'ostruct'
struct = OpenStruct.new(name: 'first', place: { animal: 'thing' })
struct.place
# => {:animal=>"thing"}
struct.place[:animal]
# => "thing"
struct.place.animal
# => NoMethodError: undefined method `animal' for {:animal=>"thing"}:Hash

因此,如果您想使用点表示法来获得struct.place.animal,您需要像以前一样创建嵌套的OpenStruct对象。但是,正如我所说,您不需要覆盖[]方法。在不覆盖[]的情况下使用您的类,我得到的是:

struct = MyOpenStruct.new(name: 'first', place: { animal: 'thing' })
# => #<MyOpenStruct name="first", place=#<MyOpenStruct animal="thing">> 
struct.place
# => #<MyOpenStruct animal="thing"> 
struct.place.animal
# => "thing" 

无论如何,如果您真的想让点表示法按您的要求工作,您可以覆盖new_ostruct_member方法,该方法在设置OpenStruct对象时在内部用于创建动态属性。你可以尝试这样的东西,但我不推荐

class MyOpenStruct < OpenStruct
  def initialize(object=nil)
    @table = {}
    @hash_table = {}
    if object
      object.each do |k,v|
        if v.is_a?(Array)
          other = Array.new()
          v.each { |e| other.push(self.class.new(entry)) }
          v = other
        end
        @table[k.to_sym] = (v.is_a?(Hash) ? self.class.new(v) : v)
        @hash_table[k.to_sym] = v
        new_ostruct_member(k)
      end
    end
  end
  def [](val)
    @hash_table[val.to_sym]
  end
  protected
  def new_ostruct_member(name)
    name = name.to_sym
    unless respond_to?(name)
      # use the overrided `[]` method, to return a hash
      define_singleton_method(name) { self[name] }
      define_singleton_method("#{name}=") { |x| modifiable[name] = x }
    end
    name
  end
end
struct = MyOpenStruct.new(name: 'first', place: { animal: 'thing' })
# => #<MyOpenStruct name="first", place=#<MyOpenStruct animal="thing">> 
struct.place
# => {:animal=>"thing"} 
struct.place[:animal]
# => "thing" 

@Doguita的回答在各个方面都是正确的。我只是想回答你的问题"如果这不可能,那么我可以打印整个对象temp的哈希吗?"是的,你可以。您只需要覆盖to_h以递归地遍历密钥和值,并将MyOpenStruct实例转换为Hash:

def to_h
  @table.each_with_object({}) do |(key, val), table|
    table[key] = to_h_convert(val)
  end
end
private
def to_h_convert(val)
  case val
  when self.class
    val.to_h
  when Hash
    val.each_with_object({}) do |(key, val), hsh|
      hsh[key] = to_h_convert(val)
    end
  when Array
    val.map {|item| to_h_convert(item) }
  else
    val
  end
end

相关内容

  • 没有找到相关文章

最新更新