# Ruby snippet to iterate through array results in error "Ruby exception occurred: undefined method \`\[\]' for #\<LogStash::Event:0x69068bc1\>\`"

**URL:** https://discuss.elastic.co/t/ruby-snippet-to-iterate-through-array-results-in-error-ruby-exception-occurred-undefined-method-for-logstash-0x69068bc1/222166
**Category:** Logstash
**Created:** [March 4, 2020, 10:07pm UTC](https://discuss.elastic.co/t/ruby-snippet-to-iterate-through-array-results-in-error-ruby-exception-occurred-undefined-method-for-logstash-0x69068bc1/222166 "2020-03-04T22:07:24Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![kaj](https://avatars.discourse-cdn.com/v4/letter/k/85e7bf/32.png) [@kaj](https://discuss.elastic.co/u/kaj)
#### Post date: [March 4, 2020, 10:07pm UTC](https://discuss.elastic.co/t/ruby-snippet-to-iterate-through-array-results-in-error-ruby-exception-occurred-undefined-method-for-logstash-0x69068bc1/222166/1 "2020-03-04T22:07:24Z")

</div>

I've got the following xpath:

```
   xpath => [
     "/record/parameters/parameter/text()", "parameter" ]

```

Parameter is a field that can have a variable number of values, from 0 to n. So it can not exist, or it can have n values. I have the following Ruby:

```
  ruby {
    code => '
    ids = event["parameter"].split(",")  
    ids.each_with_index do |value, index|           
      event["parameter_%{index}"] = "value"           
    end
    '
  }

```

The error I get in the logs is:

`[2020-03-04T16:30:32,520][ERROR][logstash.filters.ruby] Ruby exception occurred: undefined method `' for #LogStash::Event:0x69068bc1`

I'm very new to Ruby so I'm assuming that there are a couple things missing, but my immediate questions are:

1. Do I need to have a check preceeding the rest of my Ruby making sure that the parameter field exists and has values?
2. Am I using the event function incorrectly?

---

<div class="post-metadata">

### Author: ![Badger](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/badger/32/25190_2.png) [@Badger](https://discuss.elastic.co/u/Badger)
#### Post date: [March 4, 2020, 10:14pm UTC](https://discuss.elastic.co/t/ruby-snippet-to-iterate-through-array-results-in-error-ruby-exception-occurred-undefined-method-for-logstash-0x69068bc1/222166/2 "2020-03-04T22:14:30Z")

</div>

1. Yes

2. No. See [here](https://discuss.elastic.co/t/logstash-redis-filter-goes-wrong/222151/2).

---

<div class="post-metadata">

### Author: ![kaj](https://avatars.discourse-cdn.com/v4/letter/k/85e7bf/32.png) [@kaj](https://discuss.elastic.co/u/kaj)
#### Post date: [March 5, 2020, 12:26am UTC](https://discuss.elastic.co/t/ruby-snippet-to-iterate-through-array-results-in-error-ruby-exception-occurred-undefined-method-for-logstash-0x69068bc1/222166/3 "2020-03-05T00:26:45Z")

</div>

I have gotten through all the errors I was facing, but now seem to be mucking up the loop for iterating the array.

My Ruby is now:

```
ruby {
    code => '
    parametersExist = event.get("parameter")
    if parametersExist
      ids = event.get("parameter") 
      ids.each_with_index do |value, index|           
        event.set("parameter_%{index}", "value")          
      end
    end
    '
  }

```

But it just makes a literal value:

![Greenshot 2020-03-04 19.24.40](https://us1.discourse-cdn.com/elastic/original/3X/2/4/24c2c66af97db2fe46ad2462f6ea9a7662651cd2.png)

I've confirmed that the array has values (unless this isn't even an array):

![Greenshot 2020-03-04 19.25.16](https://us1.discourse-cdn.com/elastic/original/3X/6/6/66ee306e78ef4327846a04130174260bf2ee83d6.png)

---

<div class="post-metadata">

### Author: ![Badger](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/badger/32/25190_2.png) [@Badger](https://discuss.elastic.co/u/Badger)
#### Post date: [March 5, 2020, 12:42am UTC](https://discuss.elastic.co/t/ruby-snippet-to-iterate-through-array-results-in-error-ruby-exception-occurred-undefined-method-for-logstash-0x69068bc1/222166/4 "2020-03-05T00:42:22Z")

</div>

> [@kaj](#):
>
> event.set("parameter\_%{index}", "value")

That's not how you do string interpolation in ruby. sprintf references are a logstash thing, not a ruby thing. Try

```
event.set("parameter_#{index}", value)

```

As a point of style I think it would be more common to do

```
ids = event.get("parameter")
if ids
    ...

```

thus getting rid of the intermediate variable.

---

<div class="post-metadata">

### Author: ![kaj](https://avatars.discourse-cdn.com/v4/letter/k/85e7bf/32.png) [@kaj](https://discuss.elastic.co/u/kaj)
#### Post date: [March 5, 2020, 12:56am UTC](https://discuss.elastic.co/t/ruby-snippet-to-iterate-through-array-results-in-error-ruby-exception-occurred-undefined-method-for-logstash-0x69068bc1/222166/5 "2020-03-05T00:56:25Z")

</div>

That works! Thank you so much for your prompt responses. Here is the working code for anybody checking this thread later.

```
  ruby {
    code => '
    ids = event.get("parameter")
    if ids  
      ids.each_with_index do |value, index|           
        event.set("parameter_#{index}", value)          
      end
    end
    '
  }
```

---

<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: [April 2, 2020, 12:56am UTC](https://discuss.elastic.co/t/ruby-snippet-to-iterate-through-array-results-in-error-ruby-exception-occurred-undefined-method-for-logstash-0x69068bc1/222166/6 "2020-04-02T00:56:28Z")

</div>

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