# Loop through fields using ruby resource?

**URL:** https://discuss.elastic.co/t/loop-through-fields-using-ruby-resource/55557
**Category:** Logstash
**Created:** [July 14, 2016, 6:55pm UTC](https://discuss.elastic.co/t/loop-through-fields-using-ruby-resource/55557 "2016-07-14T18:55:14Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Maxwell\_Flanders](https://avatars.discourse-cdn.com/v4/letter/m/eb8c5e/32.png) [@Maxwell\_Flanders](https://discuss.elastic.co/u/Maxwell_Flanders)
#### Post date: [July 14, 2016, 6:55pm UTC](https://discuss.elastic.co/t/loop-through-fields-using-ruby-resource/55557/1 "2016-07-14T18:55:14Z")

</div>

Hi there!

I want to apply a gsub to ALL fields in my logstash object arbitrarily. I know logstash has no tool for loops like that, but I thought I could perhaps do it in a ruby resource?? Something like:

code =\> "  
event.each {|field|  
field.gsub!("replaceThis", "withThis") }  
"

however I have not as of yet been able to find the correct syntax for the loop. I've already verified that the gsub I use is good.

Any help appreciated!!! Thanks!

---

<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: [July 15, 2016, 5:54am UTC](https://discuss.elastic.co/t/loop-through-fields-using-ruby-resource/55557/2 "2016-07-15T05:54:27Z")

</div>

When you call .each on a hash the iterator will be a two-element array of a key/value pair, so field.gsub won't work. Do you want to perform the gsub on the field _names_ or the field _values_?

---

<div class="post-metadata">

### Author: ![Maxwell\_Flanders](https://avatars.discourse-cdn.com/v4/letter/m/eb8c5e/32.png) [@Maxwell\_Flanders](https://discuss.elastic.co/u/Maxwell_Flanders)
#### Post date: [July 18, 2016, 9:46am UTC](https://discuss.elastic.co/t/loop-through-fields-using-ruby-resource/55557/3 "2016-07-18T09:46:15Z")

</div>

I would like to alter the field values.

---

<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: [July 18, 2016, 9:51am UTC](https://discuss.elastic.co/t/loop-through-fields-using-ruby-resource/55557/4 "2016-07-18T09:51:02Z")

</div>

In that case I'd try this:

```ruby
event.each { |k, v|
  event[k] = v.gsub!("replaceThis", "withThis")
}

```

---

<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, 4:47am UTC](https://discuss.elastic.co/t/loop-through-fields-using-ruby-resource/55557/5 "2017-07-06T04:47:36Z")

</div>


