mardi 4 août 2015

Does ActiveSupport's Object#try method work with Thor::CoreExt::HashWithIndifferentAccess?

I'm working on a Middleman (Ruby) site. There are data objects which are instances of Thor::CoreExt::HashWithIndifferentAccess. However, it seems whenever I call try that I get nil.

I see that Thor::CoreExt::HashWithIndifferentAccess takes advantage of method_missing. Does this break how try works? Is anyone able to replicate this?

UPDATE: Here's a simple example. I'm using Middleman which provides access to data files.

# /data/people.yml
jane:
  name: Jane Doe
  age: 25
john:
  name: John Doe
  age: 25

Then in a template you have access to the data.people method which returns a Thor::CoreExt::HashWithIndifferentAccess instance:

data.people.inspect
 #=> {"jane"=>{"name"=>"Jane Doe", "age"=>25},"john"=>{"name"=>"John Doe", "age"=>25}}

data.people.class
 #=> Thor::CoreExt::HashWithIndifferentAccess

Now when I call try on anything I get nil:

data.people.jane
 #=> {"name"=>"Jane Doe", "age"=>25}

data.people.try('jane')
 #=> nil

data.people.try(:jane)
 #=> nil

data.people.try(:something_that_does_not_exist)
 #=> nil

data.people.jane.try(:name)
 #=> nil



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire