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

**URL:** <https://www.ruby-forum.com/t/tread-on-eggs-using-metaprogramming-to-set-a-variable-name/153438>\
**Category:** Ruby\
**Created:** [November 17, 2008, 8:55am UTC](https://www.ruby-forum.com/t/tread-on-eggs-using-metaprogramming-to-set-a-variable-name/153438 "2008-11-17T08:55:00Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![trevoke](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ee7513/32.png) [@trevoke](https://www.ruby-forum.com/u/trevoke)\
**Post date:** [November 17, 2008, 8:55am UTC](https://www.ruby-forum.com/t/tread-on-eggs-using-metaprogramming-to-set-a-variable-name/153438/1 "2008-11-17T08:55:00Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![trevoke](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ee7513/32.png) [@trevoke](https://www.ruby-forum.com/u/trevoke)\
**Post date:** [November 17, 2008, 11:26am UTC](https://www.ruby-forum.com/t/tread-on-eggs-using-metaprogramming-to-set-a-variable-name/153438/2 "2008-11-17T11:26:20Z")

</div>

> 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

---

<div class="post-metadata">

**Author:** ![trevoke](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ee7513/32.png) [@trevoke](https://www.ruby-forum.com/u/trevoke)\
**Post date:** [November 17, 2008, 6:10pm UTC](https://www.ruby-forum.com/t/tread-on-eggs-using-metaprogramming-to-set-a-variable-name/153438/3 "2008-11-17T18:10:48Z")

</div>

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!
