Tread on eggs.. Using metaprogramming to set a variable name?

Something like …

require ‘spreadsheet’
include Spreadsheet
workbook = Spreadsheet::Workbook.new

I create a workbook item in which I will add spreadsheets… How many

spreadsheets? That is the trick!

loc = { 1 => ‘USA’, 2 => ‘Europe’, 3=> ‘Rest_of_world’ }
loc.each_value do |name|
workbook.create_worksheet( :name => name )
end

I’d like, in the end, to have (in this example) 3 worksheets named
‘USA’, ‘Europe’ and ‘Rest_of_world’.

Is this possible? I can’t wrap my head around it… But I feel like it
must be possible.

Thanks,
–Aldric

Is this possible? I can’t wrap my head around it… But I feel like it
must be possible.

This works fine for me :

require ‘rubygems’
require ‘spreadsheet’

book = Spreadsheet::Workbook.new

loc = [‘USA’,‘Europe’,‘Rest_of_world’]
loc.each do |name|
book.create_worksheet(:name => name)
end

book.write ‘./excel-file.xls’

Your version with a hash for loc also worked for me, but I think the
above code is a little cleaner. What problems were you having exactly ?

Chris

Thanks for doing the checking I should have done :frowning:
I got so caught up in my code that I forgot to extract and test it
myself.
I had tested, but via irb, when you ask for the spreadsheet, it gives
you a very detailed Object, including name of the spreadsheet and I
wasn’t seeing it - so I assumed incorrectly that there was a problem
there. When you gave me a kick, I had no choice but to go and check
somewhere else.
Turns out the problem was, guess what, later in the code I was working
with unsupported data. :slight_smile:

Thanks again!