# Ruby code calculate problem

**URL:** <https://discuss.elastic.co/t/ruby-code-calculate-problem/117763>\
**Category:** Logstash\
**Created:** [January 31, 2018, 8:13am UTC](https://discuss.elastic.co/t/ruby-code-calculate-problem/117763 "2018-01-31T08:13:36Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![hsiuming](https://avatars.discourse-cdn.com/v4/letter/h/d78d45/32.png) [@hsiuming](https://discuss.elastic.co/u/hsiuming)\
**Post date:** [January 31, 2018, 8:13am UTC](https://discuss.elastic.co/t/ruby-code-calculate-problem/117763/1 "2018-01-31T08:13:36Z")

</div>

Hello,we now want to do Arithmetic in logstash ruby code.

The ruby code is:

ruby {  
init =\> "@@count = -1"  
code =\> "  
event.set('[test]', @@count)  
event.set('[test2]', event.get('[layers][frame\_time\_relative]').to\_f / 0.3)  
@@count = event.get('[layers][frame\_time\_relative]')  
"  
}

The frame\_time\_relative is a float type number.

 ![%E6%9C%AA%E5%91%BD%E5%90%8D](https://us1.discourse-cdn.com/elastic/original/3X/f/f/ffd8b03b30210502c829f24b0c0ce800b9b2acf5.png)

 ![%E6%9C%AA%E5%91%BD%E5%90%8D2](https://us1.discourse-cdn.com/elastic/original/3X/2/e/2eadab274e742127fbf32e9f14d56701afcecf03.png)

We find the "test2" event code function can not work.

Data in kibana only show "test1".

We set the test2 elasticsearch module as:"test2":{"type":"long","fields": {"keyword": {"ignore\_above": 256,"type": "keyword"}}}

We try this module setting or use default setting the result is same.

Is there any wrong coding?

---

<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:** [January 31, 2018, 8:21am UTC](https://discuss.elastic.co/t/ruby-code-calculate-problem/117763/2 "2018-01-31T08:21:54Z")

</div>

Please copy/paste the text instead of posting screenshots.

As the second screenshot shows the `[layers][frame_time_relative]` field is an array with a single element. To access it and convert it to a number you can do `event.get('[layers][frame_time_relative]')[0].to_f`.

---

<div class="post-metadata">

**Author:** ![hsiuming](https://avatars.discourse-cdn.com/v4/letter/h/d78d45/32.png) [@hsiuming](https://discuss.elastic.co/u/hsiuming)\
**Post date:** [January 31, 2018, 8:31am UTC](https://discuss.elastic.co/t/ruby-code-calculate-problem/117763/3 "2018-01-31T08:31:01Z")

</div>

It can works!

Thanks for your reply.

---

<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:** [February 28, 2018, 8:31am UTC](https://discuss.elastic.co/t/ruby-code-calculate-problem/117763/4 "2018-02-28T08:31:22Z")

</div>

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