# Accessing nested fields in ruby filter for math

**URL:** <https://discuss.elastic.co/t/accessing-nested-fields-in-ruby-filter-for-math/255740>\
**Category:** Logstash\
**Created:** [November 17, 2020, 5:37pm UTC](https://discuss.elastic.co/t/accessing-nested-fields-in-ruby-filter-for-math/255740 "2020-11-17T17:37:07Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Andrew22](https://avatars.discourse-cdn.com/v4/letter/a/f6c823/32.png) [@Andrew22](https://discuss.elastic.co/u/Andrew22)\
**Post date:** [November 17, 2020, 5:37pm UTC](https://discuss.elastic.co/t/accessing-nested-fields-in-ruby-filter-for-math/255740/1 "2020-11-17T17:37:07Z")

</div>

Hello,

I am having issues with this bit of code where I am trying to add 3 nested fields together and place them in a new nested field.

here is my bit of ruby code

```
ruby {
	code => "event.set('[device][disconnect][duration]', ( (event.get('[device][disconnect][hour]').to_f * 3600) + (event.get('[device][disconnect][minute]').to_f *60) + event.get('[device][disconnect][second').to_f))"
}

```

and this is the error that it is giving me

```
logstash[8322]: [2020-11-17T12:26:40,902][ERROR][logstash.filters.ruby Ruby exception occurred: wrong number of arguments calling `to_f` (given 1, expected 0)

```

any help would be great!

regards

---

<div class="post-metadata">

**Author:** ![Badger](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/badger/32/25190_2.png) [@Badger](https://discuss.elastic.co/u/Badger)\
**Post date:** [November 17, 2020, 7:09pm UTC](https://discuss.elastic.co/t/accessing-nested-fields-in-ruby-filter-for-math/255740/2 "2020-11-17T19:09:22Z")

</div>

> [@Andrew22](#):
>
> `event.get('[device][disconnect][second').to_f`

You are missing a closing ] after 'second'. You also need to change \*60 to \* 60 with a space.

---

<div class="post-metadata">

**Author:** ![Andrew22](https://avatars.discourse-cdn.com/v4/letter/a/f6c823/32.png) [@Andrew22](https://discuss.elastic.co/u/Andrew22)\
**Post date:** [November 17, 2020, 7:10pm UTC](https://discuss.elastic.co/t/accessing-nested-fields-in-ruby-filter-for-math/255740/3 "2020-11-17T19:10:55Z")

</div>

sorry for some reason that was not copied correctly. I may have deleted it by mistake. the ] is there in the config file

---

<div class="post-metadata">

**Author:** ![Badger](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/badger/32/25190_2.png) [@Badger](https://discuss.elastic.co/u/Badger)\
**Post date:** [November 17, 2020, 7:18pm UTC](https://discuss.elastic.co/t/accessing-nested-fields-in-ruby-filter-for-math/255740/4 "2020-11-17T19:18:54Z")

</div>

Oh, you replied whilst I was editing my post... Ruby is not parsing `*60` as mulitply by 60, but as an argument to .to\_f. Add a space to make it `* 60` and it will get parsed the way you want.

---

<div class="post-metadata">

**Author:** ![Andrew22](https://avatars.discourse-cdn.com/v4/letter/a/f6c823/32.png) [@Andrew22](https://discuss.elastic.co/u/Andrew22)\
**Post date:** [November 17, 2020, 7:30pm UTC](https://discuss.elastic.co/t/accessing-nested-fields-in-ruby-filter-for-math/255740/5 "2020-11-17T19:30:54Z")

</div>

thank you! I was so close...

---

<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 15, 2020, 7:31pm UTC](https://discuss.elastic.co/t/accessing-nested-fields-in-ruby-filter-for-math/255740/6 "2020-12-15T19:31:11Z")

</div>

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