# Math operations with ruby

**URL:** https://discuss.elastic.co/t/math-operations-with-ruby/55140
**Category:** Logstash
**Created:** [July 10, 2016, 10:53pm UTC](https://discuss.elastic.co/t/math-operations-with-ruby/55140 "2016-07-10T22:53:51Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Mormaii](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mormaii/32/10790_2.png) [@Mormaii](https://discuss.elastic.co/u/Mormaii)
#### Post date: [July 10, 2016, 10:53pm UTC](https://discuss.elastic.co/t/math-operations-with-ruby/55140/1 "2016-07-10T22:53:51Z")

</div>

I want to multiply or divide two fields and create a new field based on that. I've got this ruby code:

```
    ruby {
        code => "event['hit_rate'] = event['MAIN_client_req'] * event['MAIN_cache_hit']"
    }

```

When I try to use it I'm getting:

> Ruby exception occurred: undefined method `\*' for nil:NilClass {:level=\>:error}  
> Ruby exception occurred: NilClass can't be coerced into Fixnum {:level=\>:error}

This is a part of my logstash config:

> ```
> grok {
> match => { "message" => "(?<parameter>([^\s]+))" }
> match => { "message" => "(?<value>\s([0-9]+))" }
> match => { "beat" => "(?<hostname>(\w+\-\w+))" }
> break_on_match => false
> }
> 
> ```

> ```
> mutate {
> gsub => ["value", "\s", ""]
> gsub => ["parameter", "\.+", "_"]
> convert => { "value" => "integer" }
> lowercase => ["hostname"]
> add_field => { "%{parameter}" => "%{value}" }
> 
> ```

> ```
> ruby {
> code => "event['hit_rate'] = event['MAIN_client_req'] * event['MAIN_cache_hit']"
> }
> 
> ```

---

<div class="post-metadata">

### Author: ![warkolm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/warkolm/32/39224_2.png) [@warkolm](https://discuss.elastic.co/u/warkolm)
#### Post date: [July 11, 2016, 1:08am UTC](https://discuss.elastic.co/t/math-operations-with-ruby/55140/2 "2016-07-11T01:08:42Z")

</div>

If you have solved this, sharing the solution would be great. It might help someone in the future.

---

<div class="post-metadata">

### Author: ![Mormaii](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mormaii/32/10790_2.png) [@Mormaii](https://discuss.elastic.co/u/Mormaii)
#### Post date: [July 11, 2016, 1:12am UTC](https://discuss.elastic.co/t/math-operations-with-ruby/55140/3 "2016-07-11T01:12:00Z")

</div>

The problem I was having is that I wasn't convert them into integers with ruby so all I did was modify and add this (to\_i):

```
    ruby { 
        code => "event['cache_hit_rate'] = event['MAIN_client_req'].to_i / event['MAIN_cache_hit'].to_i"
    }
```

---

<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 6, 2017, 4:48am UTC](https://discuss.elastic.co/t/math-operations-with-ruby/55140/4 "2017-07-06T04:48:42Z")

</div>


