# Calculations using ruby filter in logstash is giving ruby exception error

**URL:** <https://discuss.elastic.co/t/calculations-using-ruby-filter-in-logstash-is-giving-ruby-exception-error/188423>\
**Category:** Logstash\
**Created:** [July 2, 2019, 1:02am UTC](https://discuss.elastic.co/t/calculations-using-ruby-filter-in-logstash-is-giving-ruby-exception-error/188423 "2019-07-02T01:02:55Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Pranay\_Garg](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pranay_garg/32/49232_2.png) [@Pranay\_Garg](https://discuss.elastic.co/u/Pranay_Garg)\
**Post date:** [July 2, 2019, 1:02am UTC](https://discuss.elastic.co/t/calculations-using-ruby-filter-in-logstash-is-giving-ruby-exception-error/188423/1 "2019-07-02T01:02:56Z")

</div>

I want to store multiplication of two fields in another field but when i run it gives ruby exception error. I am new to logstash and any help is appreciated. Thanks in advance for help.

mutate  
{  
add\_field =\>  
{  
"total\_price" =\> "%{product\_price}"   
}  
convert =\>  
{  
"product\_quantity" =\> "integer"  
"product\_price" =\> "integer"  
"total\_price" =\> "integer"  
}  
}  
ruby  
{  
code =\> "event['total\_price'] = event['product\_price'].to\_i \* event['product\_quantity'].to\_i"  
}

Error:  
[2019-07-02T06:26:18,169][ERROR][logstash.filters.ruby] Ruby exception occurred: undefined method `[]' for #<LogStash::Event:0x34990684> [2019-07-02T06:26:18,199][ERROR][logstash.filters.ruby] Ruby exception occurred: undefined method`' for #LogStash::Event:0xcbc1b3b  
[2019-07-02T06:26:18,220][ERROR][logstash.filters.ruby] Ruby exception occurred: undefined method `[]' for #<LogStash::Event:0x60d2e656> [2019-07-02T06:26:18,242][ERROR][logstash.filters.ruby] Ruby exception occurred: undefined method`' for #LogStash::Event:0x63762b11

---

<div class="post-metadata">

**Author:** ![Badger](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/badger/32/25190_2.png) [@Badger](https://discuss.elastic.co/u/Badger)\
**Post date:** [July 2, 2019, 1:07am UTC](https://discuss.elastic.co/t/calculations-using-ruby-filter-in-logstash-is-giving-ruby-exception-error/188423/2 "2019-07-02T01:07:04Z")

</div>

> [@Pranay\_Garg](#):
>
> ruby  
> {  
> code =\> "event['total\_price'] = event['product\_price'].to\_i \* event['product\_quantity'].to\_i"  
> }

In any version of logstash from the last couple of years you have to use the event API to reference or modify fields on the event. I have not tested it, but I think that would be

```
code => 'event.set("total_price", event.get("product_price").to_i * event.get("product_quantity").to_i)'

```

I always use ' to surround the code option so that I can use " around fields within the option, which means I can invoke string magic like interpolation.

---

<div class="post-metadata">

**Author:** ![Pranay\_Garg](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pranay_garg/32/49232_2.png) [@Pranay\_Garg](https://discuss.elastic.co/u/Pranay_Garg)\
**Post date:** [July 2, 2019, 1:10am UTC](https://discuss.elastic.co/t/calculations-using-ruby-filter-in-logstash-is-giving-ruby-exception-error/188423/3 "2019-07-02T01:10:11Z")

</div>

I tried this method before but it did'nt work due to some error but now it did. Thanks a lot for your help!

---

<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:** [July 30, 2019, 1:41am UTC](https://discuss.elastic.co/t/calculations-using-ruby-filter-in-logstash-is-giving-ruby-exception-error/188423/5 "2019-07-30T01:41:51Z")

</div>

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