# Ruby maths operation error

**URL:** <https://discuss.elastic.co/t/ruby-maths-operation-error/108966>\
**Category:** Logstash\
**Created:** [November 24, 2017, 5:58am UTC](https://discuss.elastic.co/t/ruby-maths-operation-error/108966 "2017-11-24T05:58:47Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Shekhar\_Pandey](https://avatars.discourse-cdn.com/v4/letter/s/7ab992/32.png) [@Shekhar\_Pandey](https://discuss.elastic.co/u/Shekhar_Pandey)\
**Post date:** [November 24, 2017, 5:58am UTC](https://discuss.elastic.co/t/ruby-maths-operation-error/108966/1 "2017-11-24T05:58:47Z")

</div>

Hi..  
ruby {

```
    code => "event['discount'] = [[event['mrp'].to_i - event['price'].to_i].to_f / event['mrp'].to_f ] * 100"           

```

}  
But the above code some what wrong....can anybody explain where I am wrong...

Thanks

---

<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:** [November 24, 2017, 6:37am UTC](https://discuss.elastic.co/t/ruby-maths-operation-error/108966/2 "2017-11-24T06:37:38Z")

</div>

What version of Logstash? If 5 or later see [https://www.elastic.co/guide/en/logstash/current/event-api.html](https://www.elastic.co/guide/en/logstash/current/event-api.html) for how to get and set field values.

Also, you're using square brackets when you should be using parentheses.

```
((event['mrp'].to_i - event['price'].to_i).to_f / event['mrp'].to_f ) * 100
```

---

<div class="post-metadata">

**Author:** ![Shekhar\_Pandey](https://avatars.discourse-cdn.com/v4/letter/s/7ab992/32.png) [@Shekhar\_Pandey](https://discuss.elastic.co/u/Shekhar_Pandey)\
**Post date:** [November 24, 2017, 7:14am UTC](https://discuss.elastic.co/t/ruby-maths-operation-error/108966/3 "2017-11-24T07:14:57Z")

</div>

Sir,

I edit the code as per latest logstash rule...which is given below. But still some error which are also given below:  
code =\>  
'event.set(  
"discount",(((event.get["mrp"].to\_f - event.get["price"].to\_f) / event.get["mrp"].to\_f ) \* 100)  
)'

ERROR] 2017-11-24 07:12:51.030 [[main]\>worker7] ruby - Ruby exception occurred: wrong number of arguments calling `get` (0 for 1)  
[ERROR] 2017-11-24 07:12:51.030 [[main]\>worker7] ruby - Ruby exception occurred: wrong number of arguments calling `get` (0 for 1)  
[ERROR] 2017-11-24 07:12:51.031 [[main]\>worker7] ruby - Ruby exception occurred: wrong number of arguments calling `get` (0 for 1)  
[ERROR] 2017-11-24 07:12:51.031 [[main]\>worker7] ruby - Ruby exception occurred: wrong number of arguments calling `get` (0 for 1)  
[ERROR] 2017-11-24 07:12:51.031 [[main]\>worker7] ruby - Ruby exception occurred: wrong number of arguments calling `get` (0 for 1)  
If any one know kindly let me know where I am wrong.  
Thanks

---

<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:** [November 24, 2017, 8:16am UTC](https://discuss.elastic.co/t/ruby-maths-operation-error/108966/4 "2017-11-24T08:16:06Z")

</div>

Read the documentation more carefully. There should be no square brackets after `event.get`.

---

<div class="post-metadata">

**Author:** ![Shekhar\_Pandey](https://avatars.discourse-cdn.com/v4/letter/s/7ab992/32.png) [@Shekhar\_Pandey](https://discuss.elastic.co/u/Shekhar_Pandey)\
**Post date:** [November 24, 2017, 9:11am UTC](https://discuss.elastic.co/t/ruby-maths-operation-error/108966/5 "2017-11-24T09:11:46Z")

</div>

Thanks sir...I got result. Here I am uploading my code....which is final solution. So if any body need they can use it...  
filter{  
ruby{  
code =\>  
'event.set(  
"discount",(((event.get("mrp").to\_f - event.get("price").to\_f) / event.get("mrp").to\_f ) \* 100)  
)'  
}  
}  
Thanks.

---

<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:** [December 22, 2017, 9:12am UTC](https://discuss.elastic.co/t/ruby-maths-operation-error/108966/6 "2017-12-22T09:12:24Z")

</div>

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