# Creating arrays in ruby

**URL:** <https://discuss.elastic.co/t/creating-arrays-in-ruby/102109>\
**Category:** Logstash\
**Created:** [September 28, 2017, 12:09pm UTC](https://discuss.elastic.co/t/creating-arrays-in-ruby/102109 "2017-09-28T12:09:12Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![fluffierbump](https://avatars.discourse-cdn.com/v4/letter/f/e480ec/32.png) [@fluffierbump](https://discuss.elastic.co/u/fluffierbump)\
**Post date:** [September 28, 2017, 12:09pm UTC](https://discuss.elastic.co/t/creating-arrays-in-ruby/102109/1 "2017-09-28T12:09:12Z")

</div>

I am trying to set an event with multiple records in an array as follows

```
ruby {
            code => "
            begin
                event.set('[fieldname][0]', 'This is record 0')
                event.set('[fieldname][1]', 'This is record 1')
            end"
}

```

Then I am trying to split that into two separate documents

```
split {
    field => '[fieldname]'
}

```

This doesn't give me an error but it doesn't split the field either.

---

<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:** [September 28, 2017, 1:11pm UTC](https://discuss.elastic.co/t/creating-arrays-in-ruby/102109/2 "2017-09-28T13:11:38Z")

</div>

What does the resulting event look like then? Use a `stdout { codec => rubydebug }` output.

I'd use

```
event.set('fieldname', ['Record 0', 'Record 1'])

```

to create an array field.

---

<div class="post-metadata">

**Author:** ![fluffierbump](https://avatars.discourse-cdn.com/v4/letter/f/e480ec/32.png) [@fluffierbump](https://discuss.elastic.co/u/fluffierbump)\
**Post date:** [September 28, 2017, 1:34pm UTC](https://discuss.elastic.co/t/creating-arrays-in-ruby/102109/3 "2017-09-28T13:34:05Z")

</div>

That works well. How can I add fields to that array on multiple lines? Something like

```
event.set('fieldname', ['Record 0',])
event.set('fieldname', ['Record 1',])

```

Obviously the problem with this is that the second line just overwrites the first.

I also tried

```
event.set('fieldname', ['Record 0',])
event.set('fieldname', [event.get(fieldname), 'Record 1'])

```

That just nests the first record further.

---

<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:** [September 28, 2017, 1:38pm UTC](https://discuss.elastic.co/t/creating-arrays-in-ruby/102109/4 "2017-09-28T13:38:01Z")

</div>

```
event.set('fieldname', event.get(fieldname) + ['Record 1'])
```

---

<div class="post-metadata">

**Author:** ![fluffierbump](https://avatars.discourse-cdn.com/v4/letter/f/e480ec/32.png) [@fluffierbump](https://discuss.elastic.co/u/fluffierbump)\
**Post date:** [September 28, 2017, 1:52pm UTC](https://discuss.elastic.co/t/creating-arrays-in-ruby/102109/5 "2017-09-28T13:52:28Z")

</div>

Brilliant. Thank you.

---

<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:** [October 26, 2017, 1:53pm UTC](https://discuss.elastic.co/t/creating-arrays-in-ruby/102109/6 "2017-10-26T13:53:28Z")

</div>

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