# Ruby exception occurred: can't convert String into Integer

**URL:** <https://discuss.elastic.co/t/ruby-exception-occurred-cant-convert-string-into-integer/119094>\
**Category:** Logstash\
**Created:** [February 8, 2018, 5:14pm UTC](https://discuss.elastic.co/t/ruby-exception-occurred-cant-convert-string-into-integer/119094 "2018-02-08T17:14:24Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ssasporta](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ssasporta/32/13695_2.png) [@ssasporta](https://discuss.elastic.co/u/ssasporta)\
**Post date:** [February 8, 2018, 5:14pm UTC](https://discuss.elastic.co/t/ruby-exception-occurred-cant-convert-string-into-integer/119094/1 "2018-02-08T17:14:25Z")

</div>

Hi,

This is my Ruby filter code in logstash:

```
           message_array = event.get("message").split("\n\n[")
       	   message_array.each do | index |
       	         if index!=0
       	              event.set("Rule#{index}", "[" + message_array[index])
       	         end
       	   end

```

**I am getting the error on run:**

[2018-02-08T19:01:18,716][ERROR][logstash.filters.ruby] `Ruby exception occurred: can't convert String into Integer`

The problem is in the line: `event.set("Rule#{index}", "[" + message_array[index])`

**If I do as below, it works fine:**

```
           event.set("Rule1", "[" + message_array[1])
       	   event.set("Rule2", "[" + message_array[2])

```

Can you pls help me to understand where the conversion from string to integer happens?

Thanks  
Sharon.

---

<div class="post-metadata">

**Author:** ![paz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/paz/32/28003_2.png) [@paz](https://discuss.elastic.co/u/paz)\
**Post date:** [February 13, 2018, 1:07pm UTC](https://discuss.elastic.co/t/ruby-exception-occurred-cant-convert-string-into-integer/119094/2 "2018-02-13T13:07:02Z")

</div>

In order to be able to iterate over an array's index you'll need _[each\_index](http://ruby-doc.org/core-1.9.3/Array.html#method-i-each_index)_, not _each_.

So you generally have:  
_each |item|_ : iterates over items.  
_each\_index |index|_ : iterates over indices.  
_each\_with\_index |item, index|_ : iterates over both.

As for the string to int error, it's probably due to some interpolation issue. Always try and explicitly convert types to avoid such pitfalls.

So the above would be:

```auto
message_array = event.get("message").split("\n\n[")
message_array.each_index do | index |
    if index!=0
        event.set("Rule"+index.to_s, "[" + message_array[index].to_s)
    end
end
```

or if you prefer one-liners for brevity,

```auto
message_array = event.get("message").split("\n\n[")
message_array.each_index { |index| event.set("Rule"+index.to_s, "[" + message_array[index].to_s) unless index == 0 }
```

---

<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:** [March 13, 2018, 1:07pm UTC](https://discuss.elastic.co/t/ruby-exception-occurred-cant-convert-string-into-integer/119094/3 "2018-03-13T13:07:56Z")

</div>

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