2009-06-11

Language comparisons

I am developing a list of the variations between the languages I typically use:

Syntax

Feature Mathematica Ruby AutoITScript C# VBA Fame
End of line comment N/A # ;   '  
Inline comment (* *)          

Language grammar

Feature Mathematica Ruby AutoITScript C# VBA Fame
End of line comment   # ;   '  
Inline comment (* *)          
Exceptions Check[code,onfail] begin
rescue
end
    on error  

Functions

Feature Mathematica Ruby AutoITScript C# VBA Fame
Transpose Transpose[] a[0].zip *a[1..-1]

N/A

N/A N/A N/A
   

2009-06-10

Gruff and Rmagick on Windows

I read this post that thought it would be very easy and just what I need to produce some graphs.  However not quite so easy as I am on Windows.  I tried gem install rmagick-win32 but then I went here to get the rmagick instructions.

http://rmagick.rubyforge.org/install-faq.html

Which worked straightforwardly.   I had previously installed imagemagick on my system.  So now hopefully gem install gruff will work. Now had a problem with the rmagick expecting to be:

C:\ruby\lib\ruby\gems\1.8\gems\rmagick-2.9.0-x86-mswin32\ext

which was due to not rebooting my computer.

The documentation is here.  I have been trying to draw a side bar with +ve and +ve variations for our daily email:

advanced_graph

However I couldn't initially get the numbers to go negative. However with sources soon have knocked up:

advanced_graph

2009-06-08

Ruby and Nozbe

Tested out gettting data from Nozbe (an excellent list manager using Dave Allens GTD methodology for inspiration) for getting reports from projects:

Using the ruby-nozbe gem it was easy to get basic information (the user key comes from your account under Extras -> API:

require 'nozbe'

user_key ='cccetc etc'

projs = Nozbe::Project.list(user_key)

projs.each {|p| puts "Id:>#{p.id}< Name:>#{p.name} Count:>#{p.count}<<"}

However the whole thing fell rather flat as I had coded all my projects using Project Groups and I couldn't get the project groups out of Nozbe.

2009-06-05

First YAML log file

I started with an excellent 5 minute tutorial and then added some a simple log file to my code.  Now I have a more or less functional log file writer.  I will improve it as I go on.

# A class for creating logfiles that show what has been done
# designed to be able to fail half way through

class H3Log
  @@default_dir = "C:"
  @@default_prefix = "Logfile"
  @@file_name = ""
  @@indent = 0
  @@indent_text = ""

  def H3Log.timestamp
    return Time.now.strftime('%Y-%m-%dT%H:%M:%S')
  end

  def H3Log.set_default_dir(dir)
    @@default_dir = dir
  end

  def H3Log.set_prefix( prefix)
    @@default_prefix = prefix
  end

  def H3Log.start(msg)
    @@file_name = "#{@@default_dir}\\#{@@default_prefix} #{Time.now.strftime('%Y-%m-%dT%H%M%S')}.yaml" if @@file_name = ""
    @@index_text = ""
    (@@indent*2).times { @@index_text += " "}
    @@indent += 1
    H3Log.msg("--- " + msg)
    H3Log.msg("start: #{H3Log.timestamp}")
  end

  def H3Log.end()
    H3Log.msg("end: #{H3Log.timestamp}")
  end

  def H3Log.msg(msg)
    f=File.open(@@file_name, 'a' )
    f.write("#{@@index_text}#{msg}\n")
    f.close
  end

end

begin
  puts "Start"
  H3Log.set_default_dir('O:\mytestdir)
  H3Log.start("log start")
  H3Log.end
rescue
  puts "had to be rescued"
end

#puts "Waiting for readline #{Time.now} "
#readline

Calling external programs

Calling external programs is easy:

system('C:\Program Files\Me\live\ClickSomething.exe')

This is a blocking call so it doesn't return control until the enclosed exe file has run.

However if you want this to click on error messages while you are controlling an application then you need it to return immediately and then to kill the process later, you need something like this:

require 'win32/process'

f = IO.popen('C:\Program Files\Me\live\ClickSomething.exe')

#do work which might generate error dialogs
Process.kill(1,f.pid)

I had loaded up popen3 earlier but I was using that to capture text output.

2009-06-02

Copy of nice Date Time Format in Ruby

I got this from here http://snippets.dzone.com/posts/show/2255

------------------------------------------------------------------- Time#strftime
time.strftime( string ) => string
---------------------------------------------------------------------------------
Formats time according to the directives in the given format string. Any text not listed as a directive will be passed through to the output string.

"
Format meaning:

%a - The abbreviated weekday name (``Sun'')
%A - The full weekday name (``Sunday'')
%b - The abbreviated month name (``Jan'')
%B - The full month name (``January'')
%c - The preferred local date and time representation
%d - Day of the month (01..31)
%H - Hour of the day, 24-hour clock (00..23)
%I - Hour of the day, 12-hour clock (01..12)
%j - Day of the year (001..366)
%m - Month of the year (01..12)
%M - Minute of the hour (00..59)
%p - Meridian indicator (``AM'' or ``PM'')
%S - Second of the minute (00..60)
%U - Week number of the current year,
starting with the first Sunday as the first
day of the first week (00..53)
%W - Week number of the current year,
starting with the first Monday as the first
day of the first week (00..53)
%w - Day of the week (Sunday is 0, 0..6)
%x - Preferred representation for the date alone, no time
%X - Preferred representation for the time alone, no date
%y - Year without a century (00..99)
%Y - Year with century
%Z - Time zone name
%% - Literal ``%'' character

t = Time.now
t.strftime("Printed on %m/%d/%Y") #=> "Printed on 04/09/2003"
t.strftime("at %I:%M%p") #=> "at 08:56AM"
"

2009-06-01

Getting Excel addins to work with Ruby

I was having difficulty with this until I came across this post by bbiker.  The problem is that when you open Excel with a program OLE link it has a different environment to that when you open manually.  Specifically addins might not work and need to be kicked.

I put it to work with the following function in the test code (this is non working code as it requires your own addin name) :

require 'win32ole'

class UpdateAddInFile

  def addin_installed?(ai)
    begin
      puts "Addins: #{ai}? =  #{@xl.AddIns(ai).Installed}"
      @xl.AddIns(ai).Installed=0
      @xl.AddIns(ai).Installed=1
    rescue
      puts "<><>Testing for adding #{ai} caused an exception."
    end
  end

  def initialize(thisFile)
    @xl = WIN32OLE.new('Excel.Application')
    @xl.Visible = true
    addin_installed?("Your Add-In")
    #Satisfying wait as Excel addins get installed
    dummy = @xl.workbooks.Add # Add a new work book
    dummy.Cells(1,1).value = "add in functions will now work"
  end

end

UpdateAddInFile.new('c:\\test.xls')

puts "Waiting for readline"
readline