# Ruby filter: Ruby exception occurred:undefined local variable or method \`map'

**URL:** <https://discuss.elastic.co/t/ruby-filter-ruby-exception-occurred-undefined-local-variable-or-method-map/129596>\
**Category:** Logstash\
**Created:** [April 26, 2018, 6:28am UTC](https://discuss.elastic.co/t/ruby-filter-ruby-exception-occurred-undefined-local-variable-or-method-map/129596 "2018-04-26T06:28:41Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![vinayakfutak](https://avatars.discourse-cdn.com/v4/letter/v/bb73d2/32.png) [@vinayakfutak](https://discuss.elastic.co/u/vinayakfutak)\
**Post date:** [April 26, 2018, 6:28am UTC](https://discuss.elastic.co/t/ruby-filter-ruby-exception-occurred-undefined-local-variable-or-method-map/129596/1 "2018-04-26T06:28:41Z")

</div>

I wanna run ruby script for calculating the diference between documents.here is my script

input {  
elasticsearch {  
hosts =\> "[http://192.168.55.213:9200](http://192.168.55.213:9200)"  
index =\> "test\_index"  
query =\> '{"sort": [{"logTimestamp": {"order": "asc"}}]}'  
}  
}  
filter {  
ruby {  
init =\> "@@map = {}"  
code =\> "  
map['timeDifference']= (Time.parse(event.get('logTimestamp')).to\_f - Time.parse(map['previousTime']).to\_f).round(4);  
event.set('timeDifference', map['timeDifference']);  
map['previousTime'] = event.get('logTimestamp');  
event.set('previousTime', map['previousTime']);  
"  
}  
mutate {  
remove\_field =\> ["@version","@timestamp"]  
}

}  
output {  
elasticsearch {  
document\_id =\> "%{logTimestamp}"  
document\_type =\> "test\_index"  
index =\> "test\_index"  
codec =\> "json"  
hosts =\> ["192.168.55.213:9200"]  
}  
}

But when I am trying to execute script its giving me error as  
undefined local variable or method `map' for #LogStash::Filters::Ruby:0x3b42c4a8

Also I wanna hold the logTimestamp of first document and calculate the difference when second record is received and finally store it in second record.How can I calculate the difference?

---

<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:** [April 26, 2018, 6:56am UTC](https://discuss.elastic.co/t/ruby-filter-ruby-exception-occurred-undefined-local-variable-or-method-map/129596/2 "2018-04-26T06:56:11Z")

</div>

The name of the variable you initialize via `init` is `@@map` so that's how you need to reference it in the `code` block.

---

<div class="post-metadata">

**Author:** ![vinayakfutak](https://avatars.discourse-cdn.com/v4/letter/v/bb73d2/32.png) [@vinayakfutak](https://discuss.elastic.co/u/vinayakfutak)\
**Post date:** [April 26, 2018, 7:06am UTC](https://discuss.elastic.co/t/ruby-filter-ruby-exception-occurred-undefined-local-variable-or-method-map/129596/3 "2018-04-26T07:06:19Z")

</div>

thanks for heads up @magnusbaeck How can I hold the time of first record to calculate time difference?

---

<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:** [April 26, 2018, 7:16am UTC](https://discuss.elastic.co/t/ruby-filter-ruby-exception-occurred-undefined-local-variable-or-method-map/129596/4 "2018-04-26T07:16:36Z")

</div>

Isn't that what you're doing now?

---

<div class="post-metadata">

**Author:** ![vinayakfutak](https://avatars.discourse-cdn.com/v4/letter/v/bb73d2/32.png) [@vinayakfutak](https://discuss.elastic.co/u/vinayakfutak)\
**Post date:** [April 26, 2018, 7:19am UTC](https://discuss.elastic.co/t/ruby-filter-ruby-exception-occurred-undefined-local-variable-or-method-map/129596/5 "2018-04-26T07:19:34Z")

</div>

It's working but every time I am getting same time as previous time

---

<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:** [May 24, 2018, 7:19am UTC](https://discuss.elastic.co/t/ruby-filter-ruby-exception-occurred-undefined-local-variable-or-method-map/129596/6 "2018-05-24T07:19:37Z")

</div>

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