我正在开发一个Middleman(Ruby)网站。有些数据对象是Thor::CoreExt::HashWithIndifferentAccess
的实例。但是,每当我致电try
时,我都会nil
。
我看到Thor::CoreExt::HashWithIndifferentAccess
利用method_missing
。这会破坏try
的工作方式吗?有人能够复制这个吗?
更新:这是一个简单的例子。我正在使用Middleman来访问data files。
# /data/people.yml
jane:
name: Jane Doe
age: 25
john:
name: John Doe
age: 25
然后在模板中,您可以访问返回data.people
实例的Thor::CoreExt::HashWithIndifferentAccess
方法:
data.people.inspect
#=> {"jane"=>{"name"=>"Jane Doe", "age"=>25},"john"=>{"name"=>"John Doe", "age"=>25}}
data.people.class
#=> Thor::CoreExt::HashWithIndifferentAccess
现在当我拨打try
时,我会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