ardavis ardavis - 6 months ago 9
Ruby Question

Is it possible to consolidate arguments in Ruby?

In the application I'm working on, someone else on the team wrote some code to populate cells in Excel like this:

sheet[1, 4] = "This is text"


That would fill out the first row, fourth column with "This is text".

I was thinking it might be neat to store that in a variable of some sort:

sheet[NAMED_CELL] = "This is text"


I don't know how I would set
NAMED_CELL
though. I tried simply doing:

NAMED_CELL = 1, 4


But that didn't work.

Is something like this possible?

Answer

You can use the "splat" operator to destructure an array of values into individual arguments:

x = [1,4]

cells[*x] = "This is text" # identical to cells[1,4]

The syntax you're trying to use, x = 1,4 cannot work. You cannot store two values in a variable without some kind of container such as an array, hash or class.