据我所知,geometry
数据只是一个简单的Hash
. 如果您不喜欢通过 ["key"] 在多个级别访问值的方式,您可以将 Hash 转换为 OpenStruct。
require 'ostruct'
result = Geocoder.search("New York, NY").first
# singleton class http://www.devalot.com/articles/2008/09/ruby-singleton
class << result
def ostructic_geometry
ostructly_deep self.geometry
end
private
def ostructly_deep(hash)
root = OpenStruct.new
# http://www.ruby-doc.org/stdlib-1.9.3/libdoc/ostruct/rdoc/OpenStruct.html#method-i-marshal_load
# -- from the user comment at the bottom --
# In the marchal_load() example, the Hash should have symbols as keys:
# hash = { :time => Time.now, :title => 'Birthday Party' }
load_data = hash.each_pair.inject({}) do |all, (key, value)|
value = ostructly_deep(value) if value.is_a?(Hash)
all[key.to_sym] = value # keys need to be symbols to load
all
end
root.marshal_load load_data
root
end
end
now_you_can_call_value_from_member_geometry = result.ostructic_geometry
now_you_can_call_value_from_member_geometry.bounds.northeast.lat # => 40.9152414