# How to call Ruby function in logstash filter?

**URL:** https://discuss.elastic.co/t/how-to-call-ruby-function-in-logstash-filter/107930
**Category:** Logstash
**Created:** [November 16, 2017, 12:07pm UTC](https://discuss.elastic.co/t/how-to-call-ruby-function-in-logstash-filter/107930 "2017-11-16T12:07:24Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Harim\_Tong](https://avatars.discourse-cdn.com/v4/letter/h/dc4da7/32.png) [@Harim\_Tong](https://discuss.elastic.co/u/Harim_Tong)
#### Post date: [November 16, 2017, 12:07pm UTC](https://discuss.elastic.co/t/how-to-call-ruby-function-in-logstash-filter/107930/1 "2017-11-16T12:07:24Z")

</div>

I don't want to write too much Ruby code in filter(Ruby filter) ,because it's hard to maintenance, can logstash filter call user defined function?  
Like I have a business function: **foo(x)** in the file: **my.rb** as below:

```
def foo(x)  
	#business code here
end

```

How to call this function in logstash filter?

---

<div class="post-metadata">

### Author: ![Harim\_Tong](https://avatars.discourse-cdn.com/v4/letter/h/dc4da7/32.png) [@Harim\_Tong](https://discuss.elastic.co/u/Harim_Tong)
#### Post date: [November 17, 2017, 7:02am UTC](https://discuss.elastic.co/t/how-to-call-ruby-function-in-logstash-filter/107930/2 "2017-11-17T07:02:18Z")

</div>

Can anyone help?

---

<div class="post-metadata">

### Author: ![guyboertje](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/guyboertje/32/31592_2.png) [@guyboertje](https://discuss.elastic.co/u/guyboertje)
#### Post date: [November 27, 2017, 3:55pm UTC](https://discuss.elastic.co/t/how-to-call-ruby-function-in-logstash-filter/107930/3 "2017-11-27T15:55:42Z")

</div>

The latest version of the ruby filter includes support for loading the ruby code from a file (v3.1.1, updated in LS 6.0.0)

The file needs to be in a certain format.

> <https://github.com/logstash-plugins/logstash-filter-ruby/blob/master/lib/logstash/filters/ruby.rb#L39>

  

> <https://github.com/logstash-plugins/logstash-filter-ruby/blob/master/spec/fixtures/field_multiplier.rb>

You can define as many methods as you need for better code execution and there is a micro test harness too.

---

<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 25, 2017, 3:55pm UTC](https://discuss.elastic.co/t/how-to-call-ruby-function-in-logstash-filter/107930/4 "2017-12-25T15:55:59Z")

</div>

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