检查数组元素是否存在

时间:2014-01-23 18:01:59

标签: ruby arrays

我有一个复杂的嵌套哈希数组。我正在尝试测试特定元素是否为空。

以下方法在控制台上返回NoMethodError:。empty?.nil?.blank?

这是数组(格式有点)

{
     "query"   =>"ominous",
     "sourceLanguage"   =>"en",
     "targetLanguage"   =>"en",
     "primaries"   =>   [
        {
           "type"         =>"headword",
           "terms"         =>         [
              {
                 "type"               =>"text",
                 "text"               =>"om·i·nous",
                 "language"               =>"en",
                 "labels"               =>               [
                    {
                       "text"                     =>"Adjective",
                       "title"                     =>"Part-of-speech"
                    }
                 ]
              },
              {
                 "type"               =>"phonetic",
                 "text"               =>"/ˈämənəs/",
                 "language"               =>"und"
              },
              {
                 "type"               =>"sound",
                 "text"               =>"http://www.gstatic.com/dictionary/static/sounds/de/0/ominous.mp3",
                 "language"               =>"und"
              }
           ],
           "entries"         =>         [
              {
                 "type"               =>"meaning",
                 "terms"               =>               [
                    {
                       "type"                     =>"text",
                       "text"                     =>"Giving the impression that something bad or unpleasant is going to happen; threatening; inauspicious",
                       "language"                     =>"en"
                    }
                 ],
                 "entries"               =>               [
                    {
                       "type"                     =>"example",
                       "terms"                     =>                     [
                          {
                             "type"                           =>"text",
                             "text"                           =>"there were x3cemx3eominousx3c/emx3e dark clouds gathering overhead",
                             "language"                           =>"en"
                          }
                       ]
                    }
                 ]
              }
           ]
        }
     ]
  }

例如,我知道特定元素是空的(或不存在),但是,当使用上述三种方法中的任何一种来测试它时,我得到一个NoMethodError。

示例:

2.1.0 :034 > result["primaries"][0]["entries"][0]["terms"][0]["text"]
=> "Giving the impression that something bad or unpleasant is going to happen; threatening; inauspicious" 

2.1.0 :035 > result["primaries"][0]["entries"][0]["terms"][0]["text"].empty?
=> false 

现在,我知道下一个元素是空的(不存在),所以我正在使用:

2.1.0 :036 > result["primaries"][0]["entries"][1]["terms"][0]["text"].empty?
NoMethodError: undefined method `[]' for nil:NilClass
  from (irb):36
  from /home/kyle/.rvm/gems/ruby-2.1.0/gems/railties-3.2.13/lib/rails/commands/console.rb:47:in `start'
  from /home/kyle/.rvm/gems/ruby-2.1.0/gems/railties-3.2.13/lib/rails/commands/console.rb:8:in `start'
  from /home/kyle/.rvm/gems/ruby-2.1.0/gems/railties-3.2.13/lib/rails/commands.rb:41:in `<top (required)>'
  from script/rails:6:in `require'
  from script/rails:6:in `<main>'

我还看到了用于检查文字值的.include?.in?方法,但我想检查该元素是否存在任何数据。

4 个答案:

答案 0 :(得分:1)

你可以这样做:

result["primaries"][0]["entries"][0]["terms"][0]["text"] rescue false

答案 1 :(得分:0)

你得到NoM​​ethodError的原因是result["primaries"][0]["entries"][1]为零,数组result["primaries"][0]["entries"]中只有一个元素。

答案 2 :(得分:0)

你需要这样的东西:

def recursive(arr)
   arr.is_a?(Array) ? arr.map{|x|recursive(x)} : arr
end

你的数组很大,我懒得调试这个输出。

答案 3 :(得分:-2)

也许您可以使用defined?

arr = [1,2,3]
puts :defined if defined? arr2 # puts nothing because arr2 doesn't exist
puts :defined if defined? arr  # "defined"