# USe Event API from ruby method

**URL:** <https://discuss.elastic.co/t/use-event-api-from-ruby-method/174993>\
**Category:** Logstash\
**Created:** [April 2, 2019, 12:58pm UTC](https://discuss.elastic.co/t/use-event-api-from-ruby-method/174993 "2019-04-02T12:58:44Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![ep4sh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ep4sh/32/40099_2.png) [@ep4sh](https://discuss.elastic.co/u/ep4sh)\
**Post date:** [April 2, 2019, 12:58pm UTC](https://discuss.elastic.co/t/use-event-api-from-ruby-method/174993/1 "2019-04-02T12:58:44Z")

</div>

Trying to do tagging inside my own method.

```
   def jP(str)
      begin
    	parsed = JSON.parse(str)
    	rescue => 
    		event.tag("CATCH")
    		return
      end
    end

```

But got an error:

> Ruby exception occurred: undefined local variable or method `event' for #LogStash::Filters::Ruby:0x4adf7f5

How can I run the event`s method inside of my methods? (for ex. event.set and other?)

---

<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:** [April 2, 2019, 1:40pm UTC](https://discuss.elastic.co/t/use-event-api-from-ruby-method/174993/2 "2019-04-02T13:40:07Z")

</div>

Is this in a script file? If so, event is in scope because it is passed to filter(event). If you want it to be in scope in other methods you need to pass it to them.

---

<div class="post-metadata">

**Author:** ![ep4sh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ep4sh/32/40099_2.png) [@ep4sh](https://discuss.elastic.co/u/ep4sh)\
**Post date:** [April 2, 2019, 1:49pm UTC](https://discuss.elastic.co/t/use-event-api-from-ruby-method/174993/3 "2019-04-02T13:49:18Z")

</div>

I run code with ruby filter plugin.  
Could you provide some simple examples, please?

---

<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:** [April 2, 2019, 2:05pm UTC](https://discuss.elastic.co/t/use-event-api-from-ruby-method/174993/4 "2019-04-02T14:05:47Z")

</div>

Are you passing the code to the ruby filter using the code option or the path option (i.e. a script file).

---

<div class="post-metadata">

**Author:** ![ep4sh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ep4sh/32/40099_2.png) [@ep4sh](https://discuss.elastic.co/u/ep4sh)\
**Post date:** [April 2, 2019, 2:07pm UTC](https://discuss.elastic.co/t/use-event-api-from-ruby-method/174993/5 "2019-04-02T14:07:40Z")

</div>

Oh, sorry!  
I pass the code by the code option

---

<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:** [April 2, 2019, 2:11pm UTC](https://discuss.elastic.co/t/use-event-api-from-ruby-method/174993/6 "2019-04-02T14:11:36Z")

</div>

OK. event is a local variable in-scope within the code block you pass to the filter. However, it is out of scope for functions defined within that block. This will get you an undefined local error.

```
        code => '
            def foo
                event.tag("CATCH")
            end
            foo
        '

```

You have to pass event to the function.

```
        code => '
            def foo(e)
                e.tag("CATCH")
            end
            foo event
        '
```

---

<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:** [April 30, 2019, 2:11pm UTC](https://discuss.elastic.co/t/use-event-api-from-ruby-method/174993/7 "2019-04-30T14:11:58Z")

</div>

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