# Help with Ruby code in Logstash

**URL:** https://discuss.elastic.co/t/help-with-ruby-code-in-logstash/142384
**Category:** Logstash
**Created:** [July 31, 2018, 2:21pm UTC](https://discuss.elastic.co/t/help-with-ruby-code-in-logstash/142384 "2018-07-31T14:21:32Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![espogian](https://avatars.discourse-cdn.com/v4/letter/e/4491bb/32.png) [@espogian](https://discuss.elastic.co/u/espogian)
#### Post date: [July 31, 2018, 2:21pm UTC](https://discuss.elastic.co/t/help-with-ruby-code-in-logstash/142384/1 "2018-07-31T14:21:32Z")

</div>

Hi, I have to create a new field in a Logstash filter which should be the result of a math operation between two other fields.

Given that I have defined in input section:

```
convert => { "field1" => "integer" }
convert => { "field2" => "integer" }

```

I want to create `field3 = ( field1 - field2 ) / field1`

I tried with this Ruby code:

```
ruby { code => "field3= ( event.get['field1'] - event.get['field2'] ) \ event.get['field1'];
				 event.set['field3'] = field3" }

```

But this ends up in a error:

`[ERROR][org.logstash.Logstash] java.lang.IllegalStateException: Logstash stopped processing because of an error: (SyntaxError) (ruby filter code):2: syntax error, unexpected null`

What is going wrong?

---

<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 31, 2018, 2:41pm UTC](https://discuss.elastic.co/t/help-with-ruby-code-in-logstash/142384/2 "2018-07-31T14:41:47Z")

</div>

Try this

```
    ruby { 
        code => "
            field3 = ( event.get('field1').to_f - event.get('field2').to_f ) / event.get('field1').to_f;
            event.set('field3', field3)
        " 
    }
```

---

<div class="post-metadata">

### Author: ![espogian](https://avatars.discourse-cdn.com/v4/letter/e/4491bb/32.png) [@espogian](https://discuss.elastic.co/u/espogian)
#### Post date: [July 31, 2018, 3:33pm UTC](https://discuss.elastic.co/t/help-with-ruby-code-in-logstash/142384/3 "2018-07-31T15:33:42Z")

</div>

@Badger thank you.

I would like to introduce a check in order to avoid the situation in which field1 is equal to zero.  
Do you think this is the correct approach?

```
if [field1] > 0 {
	ruby {
		code => "
			availability = ( event.get('field1').to_f - event.get('field2').to_f ) / event.get('field1').to_f;
			event.set('field3', field3)
			"
	}
}
```

---

<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 31, 2018, 3:57pm UTC](https://discuss.elastic.co/t/help-with-ruby-code-in-logstash/142384/4 "2018-07-31T15:57:03Z")

</div>

That looks reasonable.

---

<div class="post-metadata">

### Author: ![espogian](https://avatars.discourse-cdn.com/v4/letter/e/4491bb/32.png) [@espogian](https://discuss.elastic.co/u/espogian)
#### Post date: [August 7, 2018, 7:55am UTC](https://discuss.elastic.co/t/help-with-ruby-code-in-logstash/142384/5 "2018-08-07T07:55:31Z")

</div>

Hi @Badger, sorry for resuming this topic but I'm facing a little issue with the code:

```
if [field1] > 0

```

This produces this error:

```
"exception"=>"comparison of String with 0 failed"

```

The thing is strange because in the filter I have defined `field1` as integer:

```
convert => { "field1" => "integer" }

```

What am I missing?

---

<div class="post-metadata">

### Author: ![espogian](https://avatars.discourse-cdn.com/v4/letter/e/4491bb/32.png) [@espogian](https://discuss.elastic.co/u/espogian)
#### Post date: [August 7, 2018, 8:18am UTC](https://discuss.elastic.co/t/help-with-ruby-code-in-logstash/142384/6 "2018-08-07T08:18:36Z")

</div>

I found the issue. This option here:

```
csv {
	skip_header => "true"

```

Is not working. So Logstash is importing the header of the CSV and this makes some fields containing strings. Is this a known bug?

---

<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: [September 4, 2018, 8:18am UTC](https://discuss.elastic.co/t/help-with-ruby-code-in-logstash/142384/7 "2018-09-04T08:18:42Z")

</div>

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