# How to fix the Ruby code which a splatting array as parameter

**URL:** <https://forum.crystal-lang.org/t/how-to-fix-the-ruby-code-which-a-splatting-array-as-parameter/5071>\
**Category:** Help & Support\
**Created:** [November 4, 2022, 5:06am UTC](https://forum.crystal-lang.org/t/how-to-fix-the-ruby-code-which-a-splatting-array-as-parameter/5071 "2022-11-04T05:06:52Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![zw963](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/zw963/32/1623_2.png) [@zw963](https://forum.crystal-lang.org/u/zw963)\
**Post date:** [November 4, 2022, 5:06am UTC](https://forum.crystal-lang.org/t/how-to-fix-the-ruby-code-which-a-splatting-array-as-parameter/5071/1 "2022-11-04T05:06:52Z")

</div>

Following is a code example:

```crystal
instances_restarted.push(*stopped)

```

I get error message like this:

```auto
 127 | instances_restarted.push(*stopped)
                                 ^
Error: argument to splat must be a tuple, not Array(Array(Procodile::Instance | Nil))

```

The above `stopped` is a array, neither know the type of it element is, nor the size of `stopped`.

the element of stopped want to pushed into another array instances\_restarted.

At least, if this array have 100 elements, i consider write code like: `Tuple(Procodile::Instance, Procodile::Instance, Procodile::Instance, ...).from(stopped))` is not accepted.

So, how to fix this?

---

<div class="post-metadata">

**Author:** ![Blacksmoke16](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/blacksmoke16/32/1241_2.png) [@Blacksmoke16](https://forum.crystal-lang.org/u/Blacksmoke16)\
**Post date:** [November 4, 2022, 5:23am UTC](https://forum.crystal-lang.org/t/how-to-fix-the-ruby-code-which-a-splatting-array-as-parameter/5071/2 "2022-11-04T05:23:37Z")

</div>

As the error says, you can only splat a `Tuple`. I’m pretty sure you could just do `instances_restarted.concat stopped` to have the same behavior on this context tho. This would be basically the same as if you iterated over the array and `<<` each item.

---

<div class="post-metadata">

**Author:** ![straight-shoota](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/straight-shoota/32/36_2.png) [@straight-shoota](https://forum.crystal-lang.org/u/straight-shoota)\
**Post date:** [November 4, 2022, 9:21am UTC](https://forum.crystal-lang.org/t/how-to-fix-the-ruby-code-which-a-splatting-array-as-parameter/5071/3 "2022-11-04T09:21:56Z")

</div>

The solution is simply that `instances_restarted.push` must be implemented in a way that it accepts a dynamic collection instead of just a compile time splat argument.

---

<div class="post-metadata">

**Author:** ![zw963](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/zw963/32/1623_2.png) [@zw963](https://forum.crystal-lang.org/u/zw963)\
**Post date:** [November 4, 2022, 10:51am UTC](https://forum.crystal-lang.org/t/how-to-fix-the-ruby-code-which-a-splatting-array-as-parameter/5071/4 "2022-11-04T10:51:10Z")

</div>

> `instances_restarted.concat stopped` to have the same behavior on this context

Okay, thanks, i fix it use concat, but, before i ask this question here, it try it, but not work because some type issue, those error message a little confusing, at that time, I didn’t have time to research why those wired error raised.

```auto
In /home/zw963/Crystal/share/crystal/src/array.cr:754:39

 754 | (@buffer + @size).copy_from(other.to_unsafe, other_size)
                                         ^--------
Error: expected argument #1 to 'Pointer(Procodile::Instance)#copy_from' to be Pointer(NoReturn) or Pointer(Procodile::Instance), not Pointer(Array(Procodile::Instance | Nil))

Overloads are:
 - Pointer(T)#copy_from(source : Pointer(NoReturn), count : Int)
 - Pointer(T)#copy_from(source : Pointer(T), count : Int)

```

after some research, I guess i understood the purpose of this `(@buffer + @size).copy_from(other.to_unsafe, other_size)` here, and where is the @buffer or @size come from.

anyway, i consider those error not so friendly, for a user who just want to use `Array#concat`.

---

<div class="post-metadata">

**Author:** ![straight-shoota](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/straight-shoota/32/36_2.png) [@straight-shoota](https://forum.crystal-lang.org/u/straight-shoota)\
**Post date:** [November 4, 2022, 12:57pm UTC](https://forum.crystal-lang.org/t/how-to-fix-the-ruby-code-which-a-splatting-array-as-parameter/5071/5 "2022-11-04T12:57:18Z")

</div>

Yes, this error message is not very helpful. It spills implementation details instead of giving a clear high level error indication.  
Ideally this would be solved by validating generic type argument restrictions.

---

<div class="post-metadata">

**Author:** ![Blacksmoke16](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/blacksmoke16/32/1241_2.png) [@Blacksmoke16](https://forum.crystal-lang.org/u/Blacksmoke16)\
**Post date:** [November 4, 2022, 1:09pm UTC](https://forum.crystal-lang.org/t/how-to-fix-the-ruby-code-which-a-splatting-array-as-parameter/5071/6 "2022-11-04T13:09:29Z")

</div>

`Array#concat` isn’t the only thing that causes this type of error unfortunately. I think the gist of it tho is that there is a type mismatch between the array you’re concatting into and from. Looks like `stopped` may include `nil` values while `instances_restarted` doesn’t or something like that?

---

<div class="post-metadata">

**Author:** ![zw963](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/zw963/32/1623_2.png) [@zw963](https://forum.crystal-lang.org/u/zw963)\
**Post date:** [November 4, 2022, 2:35pm UTC](https://forum.crystal-lang.org/t/how-to-fix-the-ruby-code-which-a-splatting-array-as-parameter/5071/7 "2022-11-04T14:35:36Z")

</div>

> Looks like `stopped` may include `nil` values while `instances_restarted` doesn’t or something like that?

Yes, you are right, But those too many of irrelevant error messages made me ignore it.

---

<div class="post-metadata">

**Author:** ![asterite](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/asterite/32/60_2.png) [@asterite](https://forum.crystal-lang.org/u/asterite)\
**Post date:** [November 4, 2022, 3:19pm UTC](https://forum.crystal-lang.org/t/how-to-fix-the-ruby-code-which-a-splatting-array-as-parameter/5071/8 "2022-11-04T15:19:21Z")

</div>

You should always use `--error-trace` and scan the backtrace to see where the error was first introduced.

Given that Crystal is almost like a dynamic language at compile-time… in Ruby if you get a “type error” you see all the backtrace to see where, for example, you introduced a `nil` value.

In Crystal that was the case before! You could always see the backtrace. Now that is hidden behind `--error-trace` so you get errors that are confusing, like this one.

I hope the core team realizes that’s a mistake, and always show the full backtrace, and remove the `--error-trace` option.

Imagine coding in Ruby and when you get an exception you only see the last frame…

---

<div class="post-metadata">

**Author:** ![zw963](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/zw963/32/1623_2.png) [@zw963](https://forum.crystal-lang.org/u/zw963)\
**Post date:** [November 4, 2022, 3:42pm UTC](https://forum.crystal-lang.org/t/how-to-fix-the-ruby-code-which-a-splatting-array-as-parameter/5071/9 "2022-11-04T15:42:35Z")

</div>

Yes, it hidden the back trace which including the error raised from.

> I hope the core team realizes that’s a mistake, and always show the full backtrace, and remove the `--error-trace` option.

another option is, make user use --error-trace more easiler. 😄, i create a [issue](https://github.com/crystal-lang/crystal/issues/12706) several days ago, this solution is so easy even i can add it with in minutes, with not breaking everything.

In fact, i have already worked around it in my local laptop use a bash script as wrapper as i said the issue.

BTW: i still consider the key issue is, we need handle this case with a more better error message.
