# Logstash 6.0 breaks ruby event iteration

**URL:** <https://discuss.elastic.co/t/logstash-6-0-breaks-ruby-event-iteration/109084>\
**Category:** Logstash\
**Created:** [November 24, 2017, 9:46pm UTC](https://discuss.elastic.co/t/logstash-6-0-breaks-ruby-event-iteration/109084 "2017-11-24T21:46:40Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![vb4t](https://avatars.discourse-cdn.com/v4/letter/v/a698b9/32.png) [@vb4t](https://discuss.elastic.co/u/vb4t)\
**Post date:** [November 24, 2017, 9:46pm UTC](https://discuss.elastic.co/t/logstash-6-0-breaks-ruby-event-iteration/109084/1 "2017-11-24T21:46:40Z")

</div>

After upgrade to LS 6.0 following code stopped to work properly:

```
  ruby {
    code => "
      hash = event.to_hash
      hash.each do |k,v|
        if v == '-'
          event.remove(k)
        else
          if v.is_a? String
            v.strip!
            v.gsub!('`', ',')
          end
        end
      end
    "
  }

```

Following error messages are in LS logs:  
`[logstash.filters.ruby] Ruby exception occurred: undefined method 'time' for "-":String`

I am not calling method time and no event key is named time (there is just one time\_taken field). Where is the problem?

---

<div class="post-metadata">

**Author:** ![guyboertje](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/guyboertje/32/31592_2.png) [@guyboertje](https://discuss.elastic.co/u/guyboertje)\
**Post date:** [November 25, 2017, 11:08am UTC](https://discuss.elastic.co/t/logstash-6-0-breaks-ruby-event-iteration/109084/2 "2017-11-25T11:08:48Z")

</div>

This is a bug.

The workaround is to switch the LHS and RHS around.  
Reasoning:  
when v is the Timestamp the `v == '-'` is calling the `==(other)` method on the Timestamp instance and this does a naive `other.time` without checking whether other (the String `'-'`) is the same Class as `v`.  
By switching them around the `==(other) call no happens on the String class and it in not so naive - it does better equality checks.  
Working code:

```ruby
input {
  generator {
    message => '{"fld_1": "a`b ", "fld_2": "-", "fld_3": 7}'
    count => 2
  }
}

filter {
  json {
    source => "message"
  }

  ruby {
    code => "
      hash = event.to_hash
      hash.each do |k,v|
        if '-' == v
          event.remove(k)
        else
          if v.is_a? String
            v.strip!
            v.gsub!('`', ',')
            event.set(k, v)
          end
        end
      end
    "
  }
}

output {
  stdout { codec => rubydebug }
}

```

**NOTE: if you intend that the `v.gsub!('`', ',')`actually modifies the event then you must set it back into the event, because`v` is not guaranteed to always be a reference to the value in the event.**

---

<div class="post-metadata">

**Author:** ![vb4t](https://avatars.discourse-cdn.com/v4/letter/v/a698b9/32.png) [@vb4t](https://discuss.elastic.co/u/vb4t)\
**Post date:** [November 25, 2017, 12:49pm UTC](https://discuss.elastic.co/t/logstash-6-0-breaks-ruby-event-iteration/109084/3 "2017-11-25T12:49:29Z")

</div>

Thanks. Is this my bug (ruby language feature) or ruby or logstash bug (so I should create a new issue)?

---

<div class="post-metadata">

**Author:** ![guyboertje](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/guyboertje/32/31592_2.png) [@guyboertje](https://discuss.elastic.co/u/guyboertje)\
**Post date:** [November 25, 2017, 2:51pm UTC](https://discuss.elastic.co/t/logstash-6-0-breaks-ruby-event-iteration/109084/4 "2017-11-25T14:51:10Z")

</div>

Logstash bug. Please create a new issue at [https://github.com/elastic/logstash/issues](https://github.com/elastic/logstash/issues)

Please indicate what your old version of Logstash was - the one that the filter was known to work.

I had to modify the code to see the backtrace. I will add it to the issue after you created it.

Thanks.

---

<div class="post-metadata">

**Author:** ![vb4t](https://avatars.discourse-cdn.com/v4/letter/v/a698b9/32.png) [@vb4t](https://discuss.elastic.co/u/vb4t)\
**Post date:** [November 27, 2017, 6:52am UTC](https://discuss.elastic.co/t/logstash-6-0-breaks-ruby-event-iteration/109084/5 "2017-11-27T06:52:07Z")

</div>

I created following issue: [https://github.com/elastic/logstash/issues/8728](https://github.com/elastic/logstash/issues/8728)  
The latest known working version is 5.6.0

---

<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:** [December 25, 2017, 6:52am UTC](https://discuss.elastic.co/t/logstash-6-0-breaks-ruby-event-iteration/109084/6 "2017-12-25T06:52:13Z")

</div>

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