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

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
76d5176f2419e5984d7680d1951c0b5e?d=identicon&s=25 Aldric Giacomoni (Guest)
on 2008-11-17 08:55
(Received via mailing list)
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
A8486651a974d7d9ecc35d25de96c7ee?d=identicon&s=25 Chris Lowis (chrislo)
on 2008-11-17 11:26
> 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
76d5176f2419e5984d7680d1951c0b5e?d=identicon&s=25 Aldric Giacomoni (Guest)
on 2008-11-17 18:10
(Received via mailing list)
Thanks for doing the checking I should have done :(
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. :)

Thanks again!
This topic is locked and can not be replied to.