2

我有一组 OpenStruct 数据结构。但是当我尝试访问它们时,我只能访问country

array = [
 #<OpenStruct country="United States", visits="202", visitBounceRate="59.900990099009896", avgTimeOnSite="151.85148514851485", newVisits="176", percentNewVisits="87.12871287128714", pageviewsPerVisit="2.9207920792079207">, 
 #<OpenStruct country="Germany", visits="155", visitBounceRate="43.87096774193549", avgTimeOnSite="167.43870967741935", newVisits="104", percentNewVisits="67.0967741935484", pageviewsPerVisit="3.935483870967742">, 
 #<OpenStruct country="Brazil", visits="105", visitBounceRate="44.761904761904766", avgTimeOnSite="227.2095238095238", newVisits="77", percentNewVisits="73.33333333333333", pageviewsPerVisit="3.5142857142857142">, 
 #<OpenStruct country="United Kingdom", visits="71", visitBounceRate="46.478873239436616", avgTimeOnSite="216.91549295774647", newVisits="36", percentNewVisits="50.70422535211267", pageviewsPerVisit="3.464788732394366">
]

例如

array.each do |openstruct|
  put openstruct.country
end

返回所有国家,但如果我这样做

array.each do |openstruct|
  put openstruct.newVisits
end

很多空行被返回。

4

1 回答 1

0

这个案子没什么特别的。它必须是您的应用程序中较早的内容:

irb(main):002:0> require 'ostruct'
=> true
irb(main):003:0> OpenStruct.new
=> #<OpenStruct>
irb(main):004:0> os = _
=> #<OpenStruct>
irb(main):005:0> os.country = :russia
=> :russia
irb(main):006:0> os.newVisits = 60
=> 60
irb(main):007:0> os.country
=> :russia
irb(main):008:0> os.newVisits
=> 60
于 2014-11-27T14:50:49.477 回答