How to get rid of quotation marks for splatting array of arguments?


x = ["amp:", 0.5, ", ", "release:", 0.2]
print *x

outputs this:

"amp:" 0.5 ", " "release:" 0.2

but I want it to output this:

amp: 0.5 , release: 0.2

I’ve looked at many solutions on the web, and none of them seems to work, for example here. Can anyone advise how to eliminate those double quotes so I can splat arrays like this as parameters to play?

I’ve done a bit of a cursory search on this myself, and don’t yet see any ways to do what you want.
Granted I do not yet have the full picture, but it feels to me like there might be a better way to achieve your goal, (passing dynamic parameters to the play function?).

If you are not strictly limited to exactly the kind of data format that you show above, then one alternative example could be something like this:

x = [[:amp, 0.5], [:release, 0.2]].to_h
play 60, x

It may be helpful to understand a little more about what you are trying to fit this code into as a whole :slight_smile:

1 Like

I think I can totally make this work, thanks.

Yes, I want to pass dynamic parameters to play, but because the synths’ parameters can differ, I need to be able to test on those parameter names.

This looks like a pretty simple solution, and I was going to say n00b strikes again except that I don’t see it documented at the place I usually go to discover features. Googling to_h on its own turns up some sources.

1 Like

Array#to_h was introduced in Ruby 2.1 :slightly_smiling_face:


The system won’t let me write “D’OH” unless I fill the message out to 20 characters.

Haha. I know the feeling! I’ve found that it’s sometimes helpful to try to aim for more recent API docs in the search results by typing things like ‘Ruby 3 array’

I might not have quite understood what is trying to be achieved, but if the goal is to work with synth args as values before using them as options to synth, play or sample then it might make sense to work directly with maps/hashes:

x = {amp: 0.5, release: 0.2}
play 60, x

You can use as many of these as you want:

x = {amp: 0.5, release: 0.2}
y = {attack: 0.1}
play 60, x, y

The only rule to remember is to ensure that any opt hashes go before standard keyword opts:

x = {amp: 0.5, release: 0.2}
y = {attack: 0.1}
play 60, x, y, pan: 1

I hope that this helps :slight_smile:


True, I didn’t think of mentioning hashes directly :slightly_smiling_face:

1 Like

Yes, the hash thing looks like the simplest way to do it in my case, thanks.

I’ve never actually used hashes before, just arrays, so I’ll have to study up. I mean, I wonder why the hashes have to go before standard keyword opts, which I’ve verified they do.

Hashes are just simple key value pairs. It’s like a telephone directory of names (keys) to phone numbers (values). Unlike a telephone directory in typical programming languages, all the keys of hashes need to be unique - but the values don’t.

This is just how I implemented it. The keyword opts work using the concept of “all the rest of the function’s arguments” which are then gathered together and turned to a hash and then merged with any other hashes passed as arguments earlier. I actually do quite a lot of work munging the options under the hood to make things simple yet flexible :slight_smile:

1 Like