# Ruby code not updating integer

**URL:** <https://discuss.elastic.co/t/ruby-code-not-updating-integer/78791>\
**Category:** Logstash\
**Created:** [March 16, 2017, 3:45am UTC](https://discuss.elastic.co/t/ruby-code-not-updating-integer/78791 "2017-03-16T03:45:44Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![whyapenny](https://avatars.discourse-cdn.com/v4/letter/w/90db22/32.png) [@whyapenny](https://discuss.elastic.co/u/whyapenny)\
**Post date:** [March 16, 2017, 3:45am UTC](https://discuss.elastic.co/t/ruby-code-not-updating-integer/78791/1 "2017-03-16T03:45:44Z")

</div>

i am attempting to take a field and increment the number by one. I had assumed this would be an easy task but it appears to be not so trivial. I have resorted to using the ruby filter and attempting to do this here:  
`
ruby{
code => '
event.set("myLargerNumber", ("myNumber".to_i + 1))
'
}`

The issue is "myLargerNumber" is being set to 1, not to "myNumber" plus 1. In other words every rendition causes "myLargerNumber" to be "1". If i change the code as follows, it gets set to "2" every time.:

` ruby{
code => '
event.set("myLargerNumber", ("myNumber".to_i + 2))
'
}`

---

<div class="post-metadata">

**Author:** ![magnusbaeck](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/magnusbaeck/32/44943_2.png) [@magnusbaeck](https://discuss.elastic.co/u/magnusbaeck)\
**Post date:** [March 16, 2017, 5:34am UTC](https://discuss.elastic.co/t/ruby-code-not-updating-integer/78791/2 "2017-03-16T05:34:18Z")

</div>

> ```
> event.set("myLargerNumber", ("myNumber".to_i + 1))
> 
> ```

`"myNumber".to_i` means "convert the string 'myNumber' to an integer", which returns zero. Zero plus one is one. This is what you meant to do:

```
event.set("myLargerNumber", event.get("myNumber").to_i + 1)

```

---

<div class="post-metadata">

**Author:** ![whyapenny](https://avatars.discourse-cdn.com/v4/letter/w/90db22/32.png) [@whyapenny](https://discuss.elastic.co/u/whyapenny)\
**Post date:** [March 16, 2017, 3:29pm UTC](https://discuss.elastic.co/t/ruby-code-not-updating-integer/78791/3 "2017-03-16T15:29:47Z")

</div>

ahh sometimes the little oversights cause the biggest headaches. thanks Magnus.

---

<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 13, 2017, 3:30pm UTC](https://discuss.elastic.co/t/ruby-code-not-updating-integer/78791/4 "2017-04-13T15:30:03Z")

</div>

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