From: Robert Klemme Date: 2013-03-08T00:05:37+09:00 Subject: Re: Ruby Equivalent to VB's "With"? On Thu, Mar 7, 2013 at 2:07 PM, Joel Pearson wrote: > It's part of my continual effort to combine minimialism with readability > in my code. > Here's the snippet of code which is affected: > ____________________________ > class XL; end > WIN32OLE.const_load( excel, XL ) > > #Purdy it up > > #Align left 2 columns to left > sht.range('A:B').HorizontalAlignment = XL::XlLeft > > #Add a thin grid > ( Consts = [ XL::XlEdgeLeft, XL::XlEdgeTop, XL::XlEdgeBottom, > XL::XlEdgeRight, XL::XlInsideVertical, XL::XlInsideHorizontal ] ).each { > |const| sht.usedrange.Borders( const ).weight = XL::XlThin } > ____________________________ > > "XL" as a class name is my own attempt at simplifying the original > "ExcelConst". I was just wondering whether I could use these constants > without repeating "XL::" all the time. I don't know how to make them > "main" constants, and I assumed that that sort of thing would be bad > practice anyway. > > This is more about trying to work with Excel's awkward API, and the way > it insists on specifying every border of every cell as a seperate > constant. With a module you can use include $ ruby -e 'module X;Y=1;end;include X;p Y' 1 With a class you could manually copy them over $ ruby -e 'class X;Y=1;end;X.constants.each{|c| Object.const_set(c, X.const_get(c))};p Y' 1 Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/