ActiveSupport的Object#try方法是否适用于Thor :: CoreExt :: HashWithIndifferentAccess?

时间:2015-08-03 23:05:10

标签: ruby activesupport middleman thor

我正在开发一个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

0 个答案:

没有答案