Ruby code not updating integer

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))
'
}

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)
1 Like

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

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