# How to avoid instantiating same object over and over

**URL:** <https://discuss.elastic.co/t/how-to-avoid-instantiating-same-object-over-and-over/2163>\
**Category:** Logstash\
**Created:** [June 8, 2015, 8:51pm UTC](https://discuss.elastic.co/t/how-to-avoid-instantiating-same-object-over-and-over/2163 "2015-06-08T20:51:31Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![kyunam](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kyunam/32/3576_2.png) [@kyunam](https://discuss.elastic.co/u/kyunam)\
**Post date:** [June 8, 2015, 8:51pm UTC](https://discuss.elastic.co/t/how-to-avoid-instantiating-same-object-over-and-over/2163/1 "2015-06-08T20:51:31Z")

</div>

I created a filter - some kind of converter.  
In the register method, I create an instance variable (i.e, @hexGrid = HexGrid.new(cellSize)) and use it in the filter method.

In a configuration file, I chain my own filter multiple times like this.

filter {  
hexgrid {  
hex\_field =\> 'orig\_hex25'  
cellSize =\> 25  
...  
}

hexgrid {  
hex\_field =\> 'orig\_hex50'  
cellSize =\> 50  
...  
}

hexgrid {  
hex\_field =\> 'orig\_hex100'  
cellSize =\> 100  
...  
}  
}

I noticed that each time an event comes in and each time this event goes thru a configured filter, an instance variable in the register method gets created every time.

For example, if I have 3 filter configured for my own filter, 1 event comes in, then i see 3 different hexGrid instance variables created.

So, my ultimate question is, is there a way to instantiate an object once, then re-use it for subsequent events coming in?

Thanks!

---

<div class="post-metadata">

**Author:** ![kyunam](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kyunam/32/3576_2.png) [@kyunam](https://discuss.elastic.co/u/kyunam)\
**Post date:** [June 9, 2015, 10:20pm UTC](https://discuss.elastic.co/t/how-to-avoid-instantiating-same-object-over-and-over/2163/2 "2015-06-09T22:20:46Z")

</div>

Disregard this - I brain-farted.

register method only instantiates variables once PER a configured filter.

Thanks!

---

<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:** [July 6, 2017, 5:37am UTC](https://discuss.elastic.co/t/how-to-avoid-instantiating-same-object-over-and-over/2163/3 "2017-07-06T05:37:58Z")

</div>

Closed on July 6, 2017, 5:37am UTC


