# Ruby Filter Syntax

**URL:** https://discuss.elastic.co/t/ruby-filter-syntax/190839
**Category:** Logstash
**Created:** [July 16, 2019, 10:23pm UTC](https://discuss.elastic.co/t/ruby-filter-syntax/190839 "2019-07-16T22:23:50Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![wwalker](https://avatars.discourse-cdn.com/v4/letter/w/43a26b/32.png) [@wwalker](https://discuss.elastic.co/u/wwalker)
#### Post date: [July 16, 2019, 10:23pm UTC](https://discuss.elastic.co/t/ruby-filter-syntax/190839/1 "2019-07-16T22:23:51Z")

</div>

I'm trying to take a pair of fields that contain a date and subtract one from the other. However, my code is wrong somewhere and i get an error. This error occurs three times for each event, so I assume it's a syntax error in all three ruby filters I use in this pipeline.

```auto
[ERROR][logstash.filters.ruby] Ruby exception occurred: undefined method `[]' for #<LogStash::Event:0x5e20cf07>`

```

Here's what i have in the pipeline.

```auto
    ##convert old.sys_updated date/time data type to epoch
    ruby { code => "event['old.sys_updated_epoch'] = event['old.sys_updated_time'].to_i" }
    #Convert current timestamp to epoch
    ruby { code => "event['currenttime_epoch'] = event['sys_updated_on'].to_i" }
    #Calculate difference between old.sys_updated_epoch and currenttime_epoch
    ruby { code => "event['duration_epoch'] = event['currenttime_epoch'] - event['old.sys_updated_epoch']" }

```

Where is my code wrong at?

---

<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 16, 2019, 10:42pm UTC](https://discuss.elastic.co/t/ruby-filter-syntax/190839/2 "2019-07-16T22:42:31Z")

</div>

Unless you are using a very old version you should be using the [event API](https://www.elastic.co/guide/en/logstash/current/event-api.html), so

```
    ruby { code => "event.set('old.sys_updated_epoch') = event.get('old.sys_updated_time').to_i" }

```

In addition to the documented get and set methods, event has to\_hash and a sprintf method that can be used to resolve sprintf references.

---

<div class="post-metadata">

### Author: ![wwalker](https://avatars.discourse-cdn.com/v4/letter/w/43a26b/32.png) [@wwalker](https://discuss.elastic.co/u/wwalker)
#### Post date: [July 17, 2019, 2:13pm UTC](https://discuss.elastic.co/t/ruby-filter-syntax/190839/3 "2019-07-17T14:13:51Z")

</div>

Guess I should have mentioned that I am running Logstash 7.2.0. Unfortunately, that syntax threw an error.

```auto
[ERROR][org.logstash.Logstash] java.lang.IllegalStateException: Logstash stopped processing because of an error: (SyntaxError) (ruby filter code):2: syntax error, unexpected '='
 event.set('currenttime_epoch') = event.get('sys_updated_on').to_i 

```

When I change it to using square brackets, the following error is thrown for all three instances of the ruby filter I use.

```auto
ruby { code => "event.set['old.sys_updated_epoch'] = event.get['old.sys_updated_time'].to_i" }

```

```auto
[ERROR][logstash.filters.ruby] Ruby exception occurred: wrong number of arguments calling `set` (given 0, expected 2)

```

Giving another read of [this documentation](https://www.elastic.co/guide/en/logstash/current/event-api.html), I used the example at the very bottom to correct the syntax and get a functional line of code. Here's the working value.

```auto
ruby { code => 'event.set("old.sys_updated_epoch", event.get("old.sys_updated_time").to_i)' }

```

---

<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 17, 2019, 2:17pm UTC](https://discuss.elastic.co/t/ruby-filter-syntax/190839/4 "2019-07-17T14:17:10Z")

</div>

Sorry about that, I was so focused on changing the square brackets to API calls that I missed the other change required.

---

<div class="post-metadata">

### Author: ![wwalker](https://avatars.discourse-cdn.com/v4/letter/w/43a26b/32.png) [@wwalker](https://discuss.elastic.co/u/wwalker)
#### Post date: [July 17, 2019, 2:19pm UTC](https://discuss.elastic.co/t/ruby-filter-syntax/190839/5 "2019-07-17T14:19:40Z")

</div>

No worries, incorrect syntax is an easy miss, I do it CONSTANTLY.

---

<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: [August 14, 2019, 2:19pm UTC](https://discuss.elastic.co/t/ruby-filter-syntax/190839/6 "2019-08-14T14:19:47Z")

</div>

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