# Ruby exception undefined method \`each'

**URL:** <https://discuss.elastic.co/t/ruby-exception-undefined-method-each/158727>\
**Category:** Logstash\
**Created:** [November 29, 2018, 9:56am UTC](https://discuss.elastic.co/t/ruby-exception-undefined-method-each/158727 "2018-11-29T09:56:29Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Elango-mas](https://avatars.discourse-cdn.com/v4/letter/e/779978/32.png) [@Elango-mas](https://discuss.elastic.co/u/Elango-mas)\
**Post date:** [November 29, 2018, 9:56am UTC](https://discuss.elastic.co/t/ruby-exception-undefined-method-each/158727/1 "2018-11-29T09:56:29Z")

</div>

i have to check the list of values h=[2,3,7,12] in below array

details { a= [1,2,3,4] , b=[5,6,7,8] , c=[9,10] , d=[11,12,13] }

and have to create a new array as below  
details\_new { a=[2,3] , b=[7], d=[12] }

I am using Ruby code to check

```
ruby { code => '
        i =0
        cds = event.get("details")
            cds.each do |cnas|
                
                nodel = event.get("cnas")
                 nodel.each do |cnd|
                    
                    cnd=event.get("cnode")
                    if event.get(nodes).include?("cnd")
        				event.set("[new_cdetails][cds][i]","cnd")
        			end
                i +=1
            
                end
                
           end
        '
}

```

ButI getting this following error

`[logstash.filters.ruby] Ruby exception occurred: undefined method`each' for nil:NilClass`

Please can I any suggestions on this issue.

Thanks in advance!  
Elango

---

<div class="post-metadata">

**Author:** ![guyboertje](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/guyboertje/32/31592_2.png) [@guyboertje](https://discuss.elastic.co/u/guyboertje)\
**Post date:** [December 7, 2018, 10:34am UTC](https://discuss.elastic.co/t/ruby-exception-undefined-method-each/158727/2 "2018-12-07T10:34:41Z")

</div>

The Event `get` API call can return a nil, you need to check for this.

Is the `details` value a hash (JSON object)? If so, then you would have all the inner keys and values in the returned hash already.

Is this close to what you want?

```auto
input {
  generator {
    message => '{"details":{"a":[1,2,3],"b":[4,5,6],"c":[7,8,9]},"control":[2,3,6,7,8]}'
    count => 1
  }
}

filter {
  json {
    source => "message"
  }
  if "_jsonparsefailure" not in [tags] {
    ruby {
      code => '
        details_hash = event.get("details")
        control_array = event.get("control")
        return if details_hash.nil? || control_array.nil?
        adjusted_details_hash = Hash.new
        details_hash.each do |key, detail_values|
          # use array intersection operator
          adjusted_details_hash[key] = control_array & detail_values
        end
        event.set("adjusted_details", adjusted_details_hash)
      '
    }
  }
}

output {
  stdout {
    codec => rubydebug
  }
}

```

Gives:

```auto
{
             "details" => {
        "a" => [
            [0] 1,
            [1] 2,
            [2] 3
        ],
        "b" => [
            [0] 4,
            [1] 5,
            [2] 6
        ],
        "c" => [
            [0] 7,
            [1] 8,
            [2] 9
        ]
    },
             "control" => [
        [0] 2,
        [1] 3,
        [2] 6,
        [3] 7,
        [4] 8
    ],
          "@timestamp" => 2018-12-07T10:32:40.093Z,
            "sequence" => 0,
    "adjusted_details" => {
        "a" => [
            [0] 2,
            [1] 3
        ],
        "b" => [
            [0] 6
        ],
        "c" => [
            [0] 7,
            [1] 8
        ]
    },
            "@version" => "1",
             "message" => "{\"details\":{\"a\":[1,2,3],\"b\":[4,5,6],\"c\":[7,8,9]},\"control\":[2,3,6,7,8]}",
                "host" => "Elastics-MacBook-Pro.local"
}

```

---

<div class="post-metadata">

**Author:** ![guyboertje](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/guyboertje/32/31592_2.png) [@guyboertje](https://discuss.elastic.co/u/guyboertje)\
**Post date:** [December 7, 2018, 10:37am UTC](https://discuss.elastic.co/t/ruby-exception-undefined-method-each/158727/3 "2018-12-07T10:37:42Z")

</div>

NOTE: in the code sample I gave, I use the generator input with a JSON encoded string and the `json` filter only for illustration to give me an event with a presumed structure. You can focus on the ruby filter bit - but experiment with the example code by changing he JSON to be what your event looks like and change the code literals to suit.

---

<div class="post-metadata">

**Author:** ![Elango-mas](https://avatars.discourse-cdn.com/v4/letter/e/779978/32.png) [@Elango-mas](https://discuss.elastic.co/u/Elango-mas)\
**Post date:** [December 11, 2018, 6:55am UTC](https://discuss.elastic.co/t/ruby-exception-undefined-method-each/158727/4 "2018-12-11T06:55:05Z")

</div>

Thank you!

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [January 8, 2019, 6:55am UTC](https://discuss.elastic.co/t/ruby-exception-undefined-method-each/158727/5 "2019-01-08T06:55:08Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
