# Use Ruby to get field string length

**URL:** <https://discuss.elastic.co/t/use-ruby-to-get-field-string-length/241460>\
**Category:** Logstash\
**Created:** [July 16, 2020, 1:02pm UTC](https://discuss.elastic.co/t/use-ruby-to-get-field-string-length/241460 "2020-07-16T13:02:11Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![uklipse](https://avatars.discourse-cdn.com/v4/letter/u/cdc98d/32.png) [@uklipse](https://discuss.elastic.co/u/uklipse)\
**Post date:** [July 16, 2020, 1:02pm UTC](https://discuss.elastic.co/t/use-ruby-to-get-field-string-length/241460/1 "2020-07-16T13:02:11Z")

</div>

I am trying to get the string length of a dns domain name and place into a new field. I've seen several posts to use ruby to achieve this but I can't seem to get it to work. This code is placed inside the filter {} brackets. I've also tried variations where the single quotes are around all the '[dns][domain][name]' blocks. I'm getting a \_rubyexecption in the tags.

```
ruby {
  code => "event['dns']['domain']['name']['size'] = event['dns']['domain']['name'].length"
 }
```

---

<div class="post-metadata">

**Author:** ![ylasri](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ylasri/32/86120_2.png) [@ylasri](https://discuss.elastic.co/u/ylasri)\
**Post date:** [July 16, 2020, 1:05pm UTC](https://discuss.elastic.co/t/use-ruby-to-get-field-string-length/241460/2 "2020-07-16T13:05:21Z")

</div>

Use this

```
ruby {
  code => "event.set('[dns][domain][size]', event.get('[dns][domain][name]').length)"
 }
```

---

<div class="post-metadata">

**Author:** ![uklipse](https://avatars.discourse-cdn.com/v4/letter/u/cdc98d/32.png) [@uklipse](https://discuss.elastic.co/u/uklipse)\
**Post date:** [July 16, 2020, 1:13pm UTC](https://discuss.elastic.co/t/use-ruby-to-get-field-string-length/241460/3 "2020-07-16T13:13:43Z")

</div>

That did it! Thanks for the help and perhaps the fastest response I've ever seen on this site!!

---

<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:** [August 13, 2020, 1:16pm UTC](https://discuss.elastic.co/t/use-ruby-to-get-field-string-length/241460/4 "2020-08-13T13:16:52Z")

</div>

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