# Ruby script initialize variable only once

**URL:** <https://discuss.elastic.co/t/ruby-script-initialize-variable-only-once/163672>\
**Category:** Logstash\
**Created:** [January 10, 2019, 7:53am UTC](https://discuss.elastic.co/t/ruby-script-initialize-variable-only-once/163672 "2019-01-10T07:53:02Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![pohsun.teh](https://avatars.discourse-cdn.com/v4/letter/p/f08c70/32.png) [@pohsun.teh](https://discuss.elastic.co/u/pohsun.teh)\
**Post date:** [January 10, 2019, 7:53am UTC](https://discuss.elastic.co/t/ruby-script-initialize-variable-only-once/163672/1 "2019-01-10T07:53:02Z")

</div>

Sorry, not sure if my problem falls under Logstash or plain Ruby.

Using ruby script, how do I **initialize my variable only once**? I get that the script would be executed for every events, is it possible to have something like `initialize` function to only execute once?

Using `init`, I able to initialize it and use at `code` configuration. But was unable to retrieve the variable at ruby script.

Here is my current code

> init =\> "$links = Net::HTTP.get(URI('[http://localhost:8085/link/all](http://localhost:8085/link/all)'))"  
> path =\> "./filter.rb"

From the ruby script, I couldn't access the `$links` variable

Thanks in advance.

**Update**

Found out `register(params)` is the initializer

```
def register(params)
end

```

---

<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:** [February 7, 2019, 7:53am UTC](https://discuss.elastic.co/t/ruby-script-initialize-variable-only-once/163672/2 "2019-02-07T07:53:03Z")

</div>

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