# Easiest way to convert an array of strings to a splattable tuple?

**URL:** <https://forum.crystal-lang.org/t/easiest-way-to-convert-an-array-of-strings-to-a-splattable-tuple/5995>\
**Category:** Help & Support\
**Created:** [September 8, 2023, 1:04pm UTC](https://forum.crystal-lang.org/t/easiest-way-to-convert-an-array-of-strings-to-a-splattable-tuple/5995 "2023-09-08T13:04:12Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![lasso](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/lasso/32/2148_2.png) [@lasso](https://forum.crystal-lang.org/u/lasso)\
**Post date:** [September 8, 2023, 1:04pm UTC](https://forum.crystal-lang.org/t/easiest-way-to-convert-an-array-of-strings-to-a-splattable-tuple/5995/1 "2023-09-08T13:04:12Z")

</div>

Hello, I’m writing a small command-line based crystal program but I’ve run into a small problem. After parsing some command line options using OptionParser I want to call a method that takes a variable number of parameters:

```auto
my_args = ["my", "args", "from", "command", "line"]

def my_meth(*args : String)
    # do stuff with args
end

```

Coming from ruby, I kind of expected the array to be “splattable”, but reading the docs it seems like only a Tuple can be “splatted”. But I cannot find a simple way to convert my array to a tuple. There probably is a simple solution, but I can’t find it in the standard library docs or the crystal book.

Any suggestions on how to solve this problem?

Kind regards

Lars

---

<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:** [September 8, 2023, 1:07pm UTC](https://forum.crystal-lang.org/t/easiest-way-to-convert-an-array-of-strings-to-a-splattable-tuple/5995/2 "2023-09-08T13:07:27Z")

</div>

If the array of args is dynamically sized, then you just cannot do it. Maybe update your method signature to be like `args : Enumerable(String)` and just pass the array directly?

If the size of the array is known at compile time, you _could_ leverage [Tuple(\*T) - Crystal 1.9.2](https://crystal-lang.org/api/Tuple.html#from%28array%3AArray%29%3Aself-class-method), but I don’t think that’ll be the case here.

---

<div class="post-metadata">

**Author:** ![lasso](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/lasso/32/2148_2.png) [@lasso](https://forum.crystal-lang.org/u/lasso)\
**Post date:** [September 8, 2023, 1:42pm UTC](https://forum.crystal-lang.org/t/easiest-way-to-convert-an-array-of-strings-to-a-splattable-tuple/5995/3 "2023-09-08T13:42:09Z")

</div>

Thank you for the quick answer.

Since I’m getting the arguments from the command line I have no control over how many arguments I get, At the same time I don’t want to force callers of the method to wrap a single string in an array. If an array cannot be splatted and it is impossible to convert the array to a tuple it is probably just easier to overload the function.

```auto
def my_meth(arg : String)
    my_meth([arg])
end

def my_meth(args : Enumerable(String))
    # Do stuff with args
end

```

Lars

---

<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:** [September 8, 2023, 1:47pm UTC](https://forum.crystal-lang.org/t/easiest-way-to-convert-an-array-of-strings-to-a-splattable-tuple/5995/4 "2023-09-08T13:47:45Z")

</div>

Yes, you could also support both of your use cases via like:

```crystal
def foo(*args : String)
  foo args
end

def foo(args : Enumerable(String))
    # Do stuff with args
end

```

Such that you could do:

```crystal
foo "blah"
foo "bar", "baz"
foo({"a", "b", "c"})
foo(["a", "b", "c"])
foo(some_iterator)

```
