Ruby Gems?

edited February 2014 in Ruby Scripting
Hi,

I was wondering if it is possible to add Ruby Gems to Klayout? I'm particularly interested in the Axlsx gem so I can pull information from an excel file. I'm fairly new to Ruby. I've found I can add it to a stand-alone version of Ruby, but I have no idea if/how it can be added to Klayout. I'm running windows.

Thanks.

Comments

  • edited November -1

    Hi,

    I did not want to create dependencies on Ruby installations, so KLayout comes with a built-in Ruby interpreter. It is capable of loading gems, but installing them is not going to happen the usual way.

    On Linux, KLayout grabs the system-installed Ruby library and gems which are installed on the system are available to KLayout as well.

    On Windows, you may be able to redirect KLayout to another Ruby installation using the RUBYLIB environment variable, but I have not tried myself.

    For this to be successful, you'll have to install Ruby of the same version that KLayout uses (see Help/About) and preferably binary compatible with Visual C++ 2010 (which KLayout is built on). If you have that in place, you should be able to install gems and have KLayout participate by setting RUBYLIB to point to the native Ruby's gem folder.

    As I mentioned, I have not tried myself yet.

    Matthias

  • edited November -1
    Thanks for the suggestions Matthias. I might give it a try at some point. I'll let you know how I make out.
  • edited February 2014

    Hi Bob,

    Sounds like Axlsx is not included in the Ruby version.

    However if all you want to do is read values from an excel file, here is an example script I wrote that uses win32ole. Note that this works on my installation (KLayout 0.22.9) without installing or copying any extra files. To use it, make a file that might look like this where the "|" character marks the division between spreadsheet cells:

    val1 | val2 | ...
    0    | 0    | ...
    1    | 0    | ...
    1.43 | 3    | ...
    ...  | ...  | ...
    

    Now if you run the following code you will get output from the "p" lines like this:

    [["x", "y"], [0.0, 0.0], [1.0, 0.0], [1.43, 3.0]]
    [[0.0, 0.0], [1.0, 0.0], [1.43, 3.0]]
    

    Here is the code:

    module MyModule
    
      include RBA
    
      require 'win32ole'
    
      begin
        excel = WIN32OLE.connect('excel.application')
      rescue
        excel = WIN32OLE.new('excel.application')
      end
    
      excel.DisplayAlerts = false
    
      wb = excel.Workbooks.Open('C:\filepath\spreadsheet.xlsx')
    
      values_read = ''
    
      #Loop through sheets. This is ok even if you only have data on one sheet
      wb.sheets.each do |sh|
        values_read = sh.range( sh.cells( 1, 1 ), sh.cells.specialcells( 11 )).value
      end
      p values_read
    
      wb.Close(0)
      excel.Quit unless excel.visible
    
      # Remove the first (header) row, i.e. the zeroth row
      values_read = values_read[1,values_read.length-1]
      p values_read
    
      begin  
        # Do stuff here using those numbers you got from the spreadsheet
      end
    
    end
    
  • edited November -1

    Hi Bob,

    thank you for that suggestion.

    I figured out in the mean time that KLayout comes with the rubygems module included, but I have not found a way to use them to install gems. One problem is the zlib.

    I'll keep you updated.

    Matthias

  • edited February 2014

    Actually it was me ;) but you're welcome.. haha.. and I have another question..

    I didn't know win32ole would work, I just found some code online and tried it. So now I am searching for a definitive list of available code (beyond your KLayout docs) so I know what is and what is not included in Ruby in the default KLayout build for Windows.

    So is it safe to say that all the things on the right side of this page:

    http://ruby-doc.org/stdlib-1.9.3/
    

    are included, while all the gems such as those on this page:

    http://ruby-doc.org/gems/
    

    are not?

    And if I understand what you said correctly, you're not sure yet if there is a way to use gems such as these:

    http://ruby-doc.org/gems/
    

    right? (I don't need to use them, but am just making sure I understand)

    Thanks!

  • edited November -1

    Sorry, got confused :-)

    In the Windows build I included everything that came together with the 1.9.1 tarkit. I think that is what stdlib-1.9.3 covers. I noticed however, that zlib support is missing (simply because I did not have zlib installed the time I was building it).

    Regarding gems, it's basically possible to install gems, so they can be used by KLayout. But in order to install gems, you'll need a running Ruby installation. KLayout even comes with the gem library, but unfortunately it requires zlib, so gem installation does not work from inside KLayout.

    In addition, KLayout is not real Ruby but is embedding a Ruby interpreter. That is probably pretty close to a real installation, but there may be some differences (i.e. in the Config package) which prohibit installing gems directly into KLayout.

    An option would be to provide a parallel installation along with KLayout and let KLayout participate from it's repository through RUBYLIB. But you'll probably need the same Ruby than was used for KLayout. That is precisely the way it works in Linux and on this system you can simple install gems and let KLayout use them.

    I see an option to build KLayout versus a recent Ruby, include zlib this time and maybe then it's possible to install gems. But that'll take a little. But I'll try that.

    Matthias

  • edited March 2014

    Hello,

    I got somewhat further, but did not succeed yet.

    Here's my main problem:

    • KLayout is built on Qt
    • Qt supported toolchain for 64bit is MSVC VC10 only, hence KLayout is built with VS2010
    • Ruby is embedded into KLayout
    • Hence the Ruby interpreter must be build with VS2010 too

    The latter condition is not established yet, but given the experience I made so far with unstable code in mixed-system configurations, I don't want to challenge that for now.

    I succeeded in building Ruby 1.9.3 with VS2010 including the modules required for installing gems (zlib, psych). That was a configration nightmare since VC10 is never the primary target. It always required using weired build recipes and I still have not succeeded in building a libffi that Ruby would accept ...

    Anyhow, I got a Ruby1.9.3/VC10 up and running and my next plan was to build KLayout against that. Basically it should be possible then to install gems either within KLayout or borrow them from the equivalent Ruby installation.

    BUT: as mentioned VC10 is never the primary target. Hence, the axlsx gem won't install, probably because no fitting binary gem is available for nokogiri (it's installing nokogiri-1.6.1-x86-mswin32-60, but apparently that does not work).

    I'll try to use the MinGW-based RubyInstaller distribution and link against that. But as I said, I don't see a good chance this will work out.

    Matthias

  • edited November -1

    Update: I tried to link KLayout/VC10 against MinGW Ruby from RubyInstaller, but I ran into serious trouble with some standard C functions. I suspect that they are routed into MinGW via the Ruby library and that leads to a mixing of VC and MinGW code. The effect is that files are garbled for example.

    I don't want to debug that and as of now, the only option left is to build KLayout on MinGW. There appears to be some support for Qt4/MinGW64 now, and maybe this way is feasible. If somebody is willing to try that combination I can give some hints. But as of now, I won't be able to provide a ready-to-install package with a gem-enabled Ruby on Windows before 0.24.

    Matthias

  • edited March 2015

    Just a note that may help someone in the future.

    You can use (some) gems using the method described below. (Windows 8, KLayout 0.23.9)

    Simply download the source code of a gem, copy everything in its 'lib' folder into C:\Program Files\KLayout\lib\ruby\1.9.1, and now it is accessible using ruby's 'require' method.

    This of course only works on gems that are created in pure ruby, so maybe not on ones with complicated structure such as C libraries.

    So, here is a specific example: Download XmlSimple. Unzip and copy just the /lib/xmlsimple.rb file, into C:\Program Files\KLayout\lib\ruby\1.9.1. Open KLayout and the Macro IDE. Type at Macro IDE's console:

    require 'xmlsimple'
    # It will return 'true' showing that it was successful. Then you can use it. Something like this:
    config = XmlSimple.xml_in('some_xml_file.xml', { 'KeyAttr' => 'name' })
    

    Having not delved in to ruby gems before, I'm not sure if such 'pure .rb' gems are the majority or the minority. But there you go. I hope it helps someone.

Sign In or Register to comment.