# Comparing two string in ruby filter / comparing two string fields

**URL:** <https://discuss.elastic.co/t/comparing-two-string-in-ruby-filter-comparing-two-string-fields/31914>\
**Category:** Logstash\
**Created:** [October 9, 2015, 2:17pm UTC](https://discuss.elastic.co/t/comparing-two-string-in-ruby-filter-comparing-two-string-fields/31914 "2015-10-09T14:17:55Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Navneet\_Mathpal](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/navneet_mathpal/32/3677_2.png) [@Navneet\_Mathpal](https://discuss.elastic.co/u/Navneet_Mathpal)\
**Post date:** [October 9, 2015, 2:17pm UTC](https://discuss.elastic.co/t/comparing-two-string-in-ruby-filter-comparing-two-string-fields/31914/1 "2015-10-09T14:17:55Z")

</div>

Hi ,

I was trying to compare two string in logstash ruby filter inside code , it it always showing wrong result . same if I am trying in ruby IDE is working perfectly fine .  
I am trying to do something

> code =\> "   
> event['id']=File.open('D:/json/samplefile2.txt', &:readline)  
> latestid=File.open('D:/json/samplefile2.txt', &:readline)  
> previousid=File.open('D:/json/rubycheck.txt', &:readline)  
> if(latestid.eql?(previousid) )  
> event['check'] = 'oldid'  
> else  
> event['check'] = 'latestid'  
> end  
> File.open('D:/json/rubycheck.txt', 'w') {|rubycheck| rubycheck.truncate(0) }  
> previousIDfile = File.open('D:/json/rubycheck.txt', 'w')  
> previousIDfile.puts (File.open('D:/json/samplefile2.txt', &:readline))  
> previousIDfile.close  
> "

OR

> if x==y  
> event['check'] = 'oldid'  
> else  
> event['check'] = 'latestid'  
> end

It was not working so I thought to do it with logstash so I have changed it

> if[oldid] == [newid] #or I tried if[oldid] == "%{newid}"  
> {  
> mutate {  
> add\_field =\> ["status" , "old"]  
> }  
> }  
> else  
> {  
> mutate {  
> add\_field =\> ["status" , "latest"]  
> }

> ```
> }
> 
> ```

Its not working , any idea why . and how we can resolve 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:** [October 9, 2015, 2:43pm UTC](https://discuss.elastic.co/t/comparing-two-string-in-ruby-filter-comparing-two-string-fields/31914/2 "2015-10-09T14:43:58Z")

</div>

Have you verified that the `oldpid` and `newpid` fields contain the expected values, e.g. by using a `stdout { codec => rubydebug } }` output?

---

<div class="post-metadata">

**Author:** ![Navneet\_Mathpal](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/navneet_mathpal/32/3677_2.png) [@Navneet\_Mathpal](https://discuss.elastic.co/u/Navneet_Mathpal)\
**Post date:** [October 9, 2015, 2:45pm UTC](https://discuss.elastic.co/t/comparing-two-string-in-ruby-filter-comparing-two-string-fields/31914/3 "2015-10-09T14:45:40Z")

</div>

yes,

It is coming as below

> "newid" =\> "1234",  
> "oldid" =\> "1234",  
> "status" =\> "latest"

---

<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:27am UTC](https://discuss.elastic.co/t/comparing-two-string-in-ruby-filter-comparing-two-string-fields/31914/4 "2017-07-06T05:27:00Z")

</div>


