Ruby Examples

From PeformIQ Upgrade
Jump to navigation Jump to search

Skelton

#!/usr/bin/env ruby



Excel

Simple Operations

#!/usr/bin/env ruby
#
#  Purpose:  Exercise a few simple operations...
#
#--------------------------------------------------------------------------

require 'win32ole'

# You can, of course, iterate over the Workbooks collection:
# 
#    for workbook in excel.Workbooks
#       # ...code...
#    end

excel = WIN32OLE.new('Excel.Application')

excel.visible = TRUE

book = excel.Workbooks.Add();
# book = excel.ActiveWorkbook

sheet = book.Worksheets(1);

sheet.Name = "Directions"

sheet.Range('A1:D1').value = ['North','South','East','West'];

book.SaveAs('C:\temp\Workbook.xls')

book.Close

book = excel.Workbooks.Open('C:\temp\Workbook.xls')

sheet = book.Worksheets(1);

sheet.setproperty('Cells', 3, 2, 10) # => The B1 cell value is 10.

book.Save

book.Close

excel.quit

# lose the reference so that excel can be garbage collected
excel = nil

# ...and collect it.
GC.start

Two

#!/usr/bin/env ruby
#
#  Purpose:  Exercise a few simple operations...
#
#--------------------------------------------------------------------------

require 'win32ole'

excel = WIN32OLE.new('Excel.Application')

excel.visible = TRUE

book  = excel.Workbooks.Add();

sheet = book.Worksheets('Sheet1');

sheet.Name = "Directions"

sheet.Range('A1:D2').value = [['North','South','East','West'],[1,2,3,4]];

# book.Worksheets('Sheet2').delete
# book.Worksheets('Sheet3').delete

book.SaveAs('C:\temp\Workbook.xls')

book.Close

excel.quit

Three

#!/usr/bin/env ruby
#
#  Purpose:  Exercise a few simple chart operations...
#
#--------------------------------------------------------------------------

require 'win32ole'

excel = WIN32OLE.new('Excel.Application')

excel.visible = TRUE

book  = excel.Workbooks.Add();

sheet = book.Worksheets('Sheet1');

sheet.Name = "Directions"

sheet.Range('A1:D2').value = [['North','South','East','West'],[1,2,3,4]];

# book.Worksheets('Sheet2').delete
# book.Worksheets('Sheet3').delete

chart = book.Charts.add

chart.name = '3D Pie Graph'

puts chart.Type

chart.Type = -4102

#     1  Area
#     2  Horizontal Bar
#     3  Vertical Bar
#     4  Line with markers
#     5  2D Pie
#     6  2D Ring
# -4102  xl3DPie

chart.SeriesCollection(1).Name = 'XYZZY'

book.SaveAs('C:\temp\Workbook.xls')

book.Close

excel.quit

Open.rb

#!/usr/bin/env ruby

require 'win32ole'

puts "Win32OLE Demo...\n";

application = WIN32OLE.new('Excel.Application')

application.visible = TRUE

workbook  = application.Workbooks.Open('C:\QC_Reporting\wrk\SQM_Report__V1_0.xls');
worksheet = workbook.Worksheets(1);

# worksheet.Range('A1:D1').value = ['North','South','East','West'];

win.rb

#!/usr/bin/env ruby

require 'win32ole'

puts "Win32OLE Demo...\n";

application = WIN32OLE.new('Excel.Application')

application.visible = TRUE

workbook  = application.Workbooks.Add();
worksheet = workbook.Worksheets(1);

worksheet.Range('A1:D1').value = ['North','South','East','West'];

Modules

#!/usr/bin/env ruby

module Debug
  def whoAmI?
    "#{self.type.name} (\##{self.id}): #{self.to_s}"
  end
end
class Phonograph
  include Debug
  # ...
end
class EightTrack
  include Debug
  # ...
end
ph = Phonograph.new("West End Blues")
et = EightTrack.new("Surrealistic Pillow")
ph.whoAmI?      »       "Phonograph (#537766170): West End Blues"
et.whoAmI?      »       "EightTrack (#537765860): Surrealistic Pillow"
$ 
#!/usr/bin/env ruby
#
#  http://www.rubycentral.com/pickaxe/tut_modules.html
#
#===================================================================================

module Inject
  def inject(n)
     each do |value|
       n = yield(n, value)
     end
     n
  end
  def sum(initial = 0)
    inject(initial) { |n, value| n + value }
  end
  def product(initial = 1)
    inject(initial) { |n, value| n * value }
  end
end


Class Array
  include Inject
end


[ 1, 2, 3, 4, 5 ].sum      #    »   15
[ 1, 2, 3, 4, 5 ].product  #    »  120


$ cat stocks.rb 
#!/usr/bin/env ruby

require 'rubygems'
require 'pp'
require 'open-uri'
require 'fastercsv'

def historical_stock_prices(code)
   url = "http://finance.google.com/finance/historical?q=NASDAQ:#{code}&output=csv"

   data = open(url).read

   csv = FasterCSV.parse data, :headers=>true, :converters=>:numeric

   csv.map { |r| r.to_hash }
end

if __FILE__ == $0
   pp historical_stock_prices(ARGV.first)
end

code = 'GOOG'