Unable to use dot syntax for ruby hash

2019-03-09 08:47发布

I'm using net/http to pull in some json data from the Yahoo Placemaker API. After receiving the response I am performing JSON.parse on the response. This gives me a hash that looks like:

{"processingTime"=>"0.001493", "version"=>"1.4.0.526 build 111113", "documentLength"=>"25", "document"=>{"administrativeScope"=>{"woeId"=>"2503863", "type"=>"Town", "name"=>"Tampa, FL, US", "centroid"=>{"latitude"=>"27.9465", "longitude"=>"-82.4593"}}, "geographicScope"=>{"woeId"=>"2503863", "type"=>"Town", "name"=>"Tampa, FL, US", "centroid"=>{"latitude"=>"27.9465", "longitude"=>"-82.4593"}}, "localScopes"=>{"localScope"=>{"woeId"=>"2503863", "type"=>"Town", "name"=>"Tampa, FL, US (Town)", "centroid"=>{"latitude"=>"27.9465", "longitude"=>"-82.4593"}, "southWest"=>{"latitude"=>"27.8132", "longitude"=>"-82.6489"}, "northEast"=>{"latitude"=>"28.1714", "longitude"=>"-82.2539"}, "ancestors"=>[{"ancestor"=>{"woeId"=>"12587831", "type"=>"County", "name"=>"Hillsborough"}}, {"ancestor"=>{"woeId"=>"2347568", "type"=>"State", "name"=>"Florida"}}, {"ancestor"=>{"woeId"=>"23424977", "type"=>"Country", "name"=>"United States"}}]}}, "extents"=>{"center"=>{"latitude"=>"27.9465", "longitude"=>"-82.4593"}, "southWest"=>{"latitude"=>"27.8132", "longitude"=>"-82.6489"}, "northEast"=>{"latitude"=>"28.1714", "longitude"=>"-82.2539"}}, "placeDetails"=>{"placeId"=>"1", "place"=>{"woeId"=>"2503863", "type"=>"Town", "name"=>"Tampa, FL, US", "centroid"=>{"latitude"=>"27.9465", "longitude"=>"-82.4593"}}, "placeReferenceIds"=>"1", "matchType"=>"0", "weight"=>"1", "confidence"=>"8"}, "referenceList"=>{"reference"=>{"woeIds"=>"2503863", "placeReferenceId"=>"1", "placeIds"=>"1", "start"=>"15", "end"=>"20", "isPlaintextMarker"=>"1", "text"=>"Tampa", "type"=>"plaintext", "xpath"=>""}}}}

I am able to access elements by doing things like jsonResponse['version'] but I am not able to do jsonResponse.version. Why is this?

标签: ruby syntax hash
8条回答
我命由我不由天
2楼-- · 2019-03-09 09:29

If it's in Rspec stubs will work too.

let(:item) { stub(current: 1, total: 1) } 
查看更多
Evening l夕情丶
3楼-- · 2019-03-09 09:33

Because Hash doesn't have a version method.

查看更多
看我几分像从前
4楼-- · 2019-03-09 09:47

Ruby hashes don't work like this natively, but the HashDot gem would work for this.

HashDot allows dot notation syntax use on hashes. It also works on json strings that have been re-parsed with JSON.parse.

require 'hash_dot'

hash = {b: {c: {d: 1}}}.to_dot
hash.b.c.d => 1

json_hash = JSON.parse(hash.to_json)
json_hash.b.c.d => 1
查看更多
乱世女痞
5楼-- · 2019-03-09 09:48

Why not, you can do this via metaprogramming

module LookLikeJSON
  def method_missing(meth, *args, &block)
    if has_key?(meth.to_s)
      self[meth.to_s]
    else
      raise NoMethodError, 'undefined method #{meth} for #{self}' 
    end
  end
end

h = {"processingTime"=>"0.001493", "version"=>"1.4.0.526 build 111113", "documentLength"=>"25"}
h.extend(LookLikeJSON)
h.processingTime #=> "0.001493"
查看更多
迷人小祖宗
6楼-- · 2019-03-09 09:52

OpenStruct will work well for a pure hash, but for hashes with embeded arrays or other hashes, the dot syntax will choke. I came across this solution, which works well without loading in another gem: https://coderwall.com/p/74rajw/convert-a-complex-nested-hash-to-an-object basic steps are:

data = YAML::load(File.open("your yaml file"))
json_data = data.to_json
mystr = JSON.parse(json_data,object_class: OpenStruct)

you can now access all objects in mystr using dot syntax.

查看更多
你好瞎i
7楼-- · 2019-03-09 09:53

That is a JavaScript feature, not a Ruby feature. In Ruby, to use a "dot syntax", the object would need to respond to those methods. Ruby hashes use the #[](key) method to access elements.

查看更多
登录 后发表回答