# Methods in Logstash Ruby

**URL:** <https://discuss.elastic.co/t/methods-in-logstash-ruby/102144>\
**Category:** Logstash\
**Created:** [September 28, 2017, 3:56pm UTC](https://discuss.elastic.co/t/methods-in-logstash-ruby/102144 "2017-09-28T15:56:51Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![fluffierbump](https://avatars.discourse-cdn.com/v4/letter/f/e480ec/32.png) [@fluffierbump](https://discuss.elastic.co/u/fluffierbump)\
**Post date:** [September 28, 2017, 3:56pm UTC](https://discuss.elastic.co/t/methods-in-logstash-ruby/102144/1 "2017-09-28T15:56:51Z")

</div>

I'm trying to get methods working in Logstash Ruby and I'm starting with a very simple one

```
def output_something(value)
    puts value
end

```

Then I call the method and try to create a field with a value from it

`event.set('[new_field]', output_something('test_text'))`

It creates the new field but the value for it is nil

`"new_field" => nil,`

---

<div class="post-metadata">

**Author:** ![magnusbaeck](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/magnusbaeck/32/44943_2.png) [@magnusbaeck](https://discuss.elastic.co/u/magnusbaeck)\
**Post date:** [September 28, 2017, 7:15pm UTC](https://discuss.elastic.co/t/methods-in-logstash-ruby/102144/2 "2017-09-28T19:15:43Z")

</div>

This is expected since your function returns nil. `puts` prints a string to stdout, it doesn't cause the string to be returned from the function.

---

<div class="post-metadata">

**Author:** ![fluffierbump](https://avatars.discourse-cdn.com/v4/letter/f/e480ec/32.png) [@fluffierbump](https://discuss.elastic.co/u/fluffierbump)\
**Post date:** [September 29, 2017, 1:17pm UTC](https://discuss.elastic.co/t/methods-in-logstash-ruby/102144/3 "2017-09-29T13:17:28Z")

</div>

It seems I'm also not putting the method in the right place, and I can't find an example in the documentation.

My current code looks like this

```
ruby {
            code => "
            def testcasebuilder()
                for i in 0..5
                    event.set('new_field', event.get('new_field') + ['test'])
                end
            end
            begin
                event.set('new_field', ['test'])
                testcasebuilder()
            end"
}

```

I've also tried this

```
ruby {
            code => "
            begin
                def testcasebuilder()
                    for i in 0..5
                        event.set('new_field', event.get('new_field') + ['test'])
                    end
                end
                event.set('new_field', ['test'])
                testcasebuilder()
            end"
}

```

Neither of these work. Where should I put my method so that I can call it?

---

<div class="post-metadata">

**Author:** ![magnusbaeck](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/magnusbaeck/32/44943_2.png) [@magnusbaeck](https://discuss.elastic.co/u/magnusbaeck)\
**Post date:** [September 29, 2017, 3:18pm UTC](https://discuss.elastic.co/t/methods-in-logstash-ruby/102144/4 "2017-09-29T15:18:33Z")

</div>

Your code looks okay to me but is there any particular reason you need to use a function instead of just inlining the code?

---

<div class="post-metadata">

**Author:** ![fluffierbump](https://avatars.discourse-cdn.com/v4/letter/f/e480ec/32.png) [@fluffierbump](https://discuss.elastic.co/u/fluffierbump)\
**Post date:** [September 29, 2017, 4:04pm UTC](https://discuss.elastic.co/t/methods-in-logstash-ruby/102144/5 "2017-09-29T16:04:43Z")

</div>

I'm trying to walk through a nested object to get all the fields with a certain name added to a single field, then I will split that field.

My idea was to create a recursive method to walk through the object.

---

<div class="post-metadata">

**Author:** ![bishplz](https://avatars.discourse-cdn.com/v4/letter/b/7bcc69/32.png) [@bishplz](https://discuss.elastic.co/u/bishplz)\
**Post date:** [October 20, 2017, 5:44pm UTC](https://discuss.elastic.co/t/methods-in-logstash-ruby/102144/6 "2017-10-20T17:44:15Z")

</div>

Have you found a way to do this recursively ? I am in a similar situation where the path to a nested object may vary across multiple events.

---

<div class="post-metadata">

**Author:** ![Martin\_Cleaver](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/martin_cleaver/32/23713_2.png) [@Martin\_Cleaver](https://discuss.elastic.co/u/Martin_Cleaver)\
**Post date:** [November 2, 2017, 9:18pm UTC](https://discuss.elastic.co/t/methods-in-logstash-ruby/102144/7 "2017-11-02T21:18:11Z")

</div>

> Your code looks okay to me but is there any particular reason you need to use a function instead of just inlining the code?

I (too) want to use a function to keep the structure of my code clean.

---

<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:** [November 30, 2017, 9:18pm UTC](https://discuss.elastic.co/t/methods-in-logstash-ruby/102144/8 "2017-11-30T21:18:18Z")

</div>

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