ardavis ardavis - 2 years ago 60
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
though. I tried simply doing:


But that didn't work.

Is something like this possible?

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download