# Ruby Code Exception

**URL:** https://discuss.elastic.co/t/ruby-code-exception/322155
**Category:** Logstash
**Created:** [December 29, 2022, 12:22pm UTC](https://discuss.elastic.co/t/ruby-code-exception/322155 "2022-12-29T12:22:31Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Rohit\_Kumbhar](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rohit_kumbhar/32/67682_2.png) [@Rohit\_Kumbhar](https://discuss.elastic.co/u/Rohit_Kumbhar)
#### Post date: [December 29, 2022, 12:22pm UTC](https://discuss.elastic.co/t/ruby-code-exception/322155/1 "2022-12-29T12:22:31Z")

</div>

Hi Team,

I am getting  
Ruby exception occurred: undefined method `length' for nil  
error while using the following Ruby Code

```auto
ruby {
    code => '
      names = event.get("[message]").split(".")
      event.set("number_of_elements", event.get("[names]").length)
      event.set("[tp]", names)
    '
  }

```

message =\> "bob.alice.jessie.tommy"

Please help me to get this

I am trying to get length of the [names] array

Thanks!

---

<div class="post-metadata">

### Author: ![leandrojmp](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/leandrojmp/32/107231_2.png) [@leandrojmp](https://discuss.elastic.co/u/leandrojmp)
#### Post date: [December 29, 2022, 12:26pm UTC](https://discuss.elastic.co/t/ruby-code-exception/322155/2 "2022-12-29T12:26:01Z")

</div>

> [@Rohit\_Kumbhar](#):
>
> Ruby exception occurred: undefined method `length' for nil

This means that the value of `[names]` is null.

Do you have a field named `names` in your event?

What you probably want is the length of the variable `names`, you should try to use `names.length`, using `event.get("[names]").length` will try to get the length of the `names` field from your document.

---

<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: [January 26, 2023, 12:26pm UTC](https://discuss.elastic.co/t/ruby-code-exception/322155/3 "2023-01-26T12:26:47Z")

</div>

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