# Ruby into file

**URL:** <https://discuss.elastic.co/t/ruby-into-file/338102>\
**Category:** Logstash\
**Created:** [July 11, 2023, 12:18pm UTC](https://discuss.elastic.co/t/ruby-into-file/338102 "2023-07-11T12:18:22Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![hofrichterovak](https://avatars.discourse-cdn.com/v4/letter/h/ba9def/32.png) [@hofrichterovak](https://discuss.elastic.co/u/hofrichterovak)\
**Post date:** [July 11, 2023, 12:18pm UTC](https://discuss.elastic.co/t/ruby-into-file/338102/1 "2023-07-11T12:18:22Z")

</div>

Hello,

I read the documentation about the ruby script and I did not correctly understand the conversion of the ruby script into a file.

If I have a converted ruby script into a file, do I have to rewrite the script into classes?

For example, I wanted to put this ruby script in a file, but it didn't work there at all. When I have this in filter section, it works:

> ```
> ruby {
> code => "
> event.set('[event][duration_seconds]', event.get('[event][duration]').to_f / 1000)
> "} 
> 
> ```

I put it into the file. In the filter was:

> ruby {  
> path =\> "{{logstash.scripts\_path}}/count.rb"  
> }

The content of the file was:

> ```
> event.set('[event][duration_seconds]', event.get('[event][duration]').to_f / 1000)
> 
> ```

Why it didn´t work?

Thank you

Regards,  
Catherine

---

<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:** [July 11, 2023, 4:25pm UTC](https://discuss.elastic.co/t/ruby-into-file/338102/2 "2023-07-11T16:25:40Z")

</div>

To convert an inline script to a file you need to define two functions. Try creating a file containing

```auto
def register (params)
end

def filter (event)
    event.set("[event][duration_seconds]", event.get("[event][duration]").to_f / 1000)
    return [event]
end

```

---

<div class="post-metadata">

**Author:** ![hofrichterovak](https://avatars.discourse-cdn.com/v4/letter/h/ba9def/32.png) [@hofrichterovak](https://discuss.elastic.co/u/hofrichterovak)\
**Post date:** [July 12, 2023, 7:29am UTC](https://discuss.elastic.co/t/ruby-into-file/338102/3 "2023-07-12T07:29:58Z")

</div>

I understand. Thank you so much! 🙂  
Is possible to add also ruby id? I mean: **id =\> "X1\_RUBY"**

ruby {  
**id =\> "X1\_RUBY"**  
code =\> "  
event.set('[event][duration\_seconds]', event.get('[event][duration]').to\_f / 1000)  
"}

---

<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:** [July 12, 2023, 2:45pm UTC](https://discuss.elastic.co/t/ruby-into-file/338102/4 "2023-07-12T14:45:44Z")

</div>

> [@hofrichterovak](#):
>
> Is possible to add also ruby id? I mean: **id =\> "X1\_RUBY"**

Yes, id is a common option that all filters support.

---

<div class="post-metadata">

**Author:** ![hofrichterovak](https://avatars.discourse-cdn.com/v4/letter/h/ba9def/32.png) [@hofrichterovak](https://discuss.elastic.co/u/hofrichterovak)\
**Post date:** [July 17, 2023, 9:27am UTC](https://discuss.elastic.co/t/ruby-into-file/338102/5 "2023-07-17T09:27:23Z")

</div>

Thank you so much. It works all perfectly. 🙂

Regards,  
Catherine

---

<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 14, 2023, 9:27am UTC](https://discuss.elastic.co/t/ruby-into-file/338102/6 "2023-08-14T09:27:33Z")

</div>

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