Ruby for research

Ruby is an interpreted object-oriented scripting language. It is quick and easy programming, and has many features to process text files and to do system management tasks. Here, I show how to install and use Ruby, and show several scripts which help my research. Please inform me, if you find some bugs, or you want me to modify in order to use more confortably, or you have some ideas to make new Ruby scripts which help your research.



There are some Ruby-cygwin, Ruby-mswin32, Ruby-mingw, and so on. Ruby-cygwin is now a part of Cygwin. To install, at first, please get 'setup.exe' from cygwin site by selecting 'Install or update now!'. Run 'setup.exe' and select some options. In the 'Select Packages' stage, at least 'Base-cygwin' and 'Interpriters-ruby' should be selected, and install them. Add path, for example 'c:\cygwin\bin' to the environment. To install Ruby-mswin32 or Ruby-mingw, please download binary archive and expand it, and put it at a proper position. And then, add path. To confirm proper installation, type 'ruby -v' in a command line, which displays the version of ruby. For more information, see Ruby site.

Mac OS X

Ruby is already installed. To confirm that, type 'ruby -v' in a terminal, and then it displays the version of ruby. To use current version, see Ruby site.


It depends on the package. For Debian, just type 'apt-get install ruby'.


To run a script, for example 'test.rb', type 'ruby test.rb' in a command prompt (Windows), or in a terminal (Mac OS X, Linux). In the case of Mac OS X, please use the UNIX return code (0Ah) in ruby scripts, since other return code makes an error. Another method to use ruby is to make a batch file (Windows) or a shell script, which describes 'ruby test.rb'. In the case of a batch file in Windows, you can use 'drag and drop'. Make a batch file that contains 'ruby test.rb %1', and make a short cut and set a working folder, then the name of a file which is dropped to the short cut can be used as ARGV[0] in 'test.rb'. For shell script in Mac OS X, make a file which contains '#!/bin/bash' as a first line and following commonds like 'ruby test.rb'. Please change the mode by executing 'chmod +x filename' in a terminal. After reassigning the file to a terminal, you can run the shell script by a double click. Please note that the working directory is user folder, then you have to change folder.

Scripts for research

I show some scripts, which I use in my research. These may help your research, or help you to develope your own Ruby scripts.

Data Processing


This script calculate formula weight of input formula, repeatedly. Fractional numbers are also be dealt with. But, please do not use brackets in present version. Atomic weight data file formula.dat should be placed in the same folder.


This script calculates valence of each element. It determines valence from atoms which have only typical oxidation number. Please modify normal valence data in this script, if the data is not suitable for your system. The valence of earlier atom in the data has priority. Please do not use brackets in present version.


This script process M-T measurement data obtained by Quantum Design Magnetic Property Measuring System (MPMS). When using MPMS, please input sample name(formula), sample weight, which is used to calculate magnetic susceptibility. If you input weight of wrapping film and/or OHP sheet as comment, like 'with wrap 1mg', 'with ohp 1mg', the background data from these are automatically subtracted. (Please note the these background data are those which I use, but I think the values are not so much different.) Run this script, and input data file name without '.dat', this script make new file with extention '.txt'. This text file is written by Igor text format to draw M-T graph. If you use Wave Matrics Igor to draw graphs, only you have to do is drag and drop this text file to Igor. Otherwise, you can extract data from this file, and please draw graph using other software. Data file and atomic weight data file formula.dat should be placed in the same folder.



This file difines functions for Montgomery method to calculate anisotropic resistivity. The function 'rho' is the main function, which leads resistivity from resistance and dimensions. Please declare as require 'montg' at the begining of your script, and write script to read file, calculate and output file. Please note that the functions used is described as a searies expansion, and the convergence is not good in some range of b/a and c/a.


This script convert X-ray powder diffraction data measured by EXPRESS to text file. In detail the output text file is written in Igor text format, and if you drag this file into Igor, then a graph is automatically drawn. Of cource, you can convert to text file by using the utility contained in the package. When you get data files through FTP, and you have to convert on your PC, this script help you to draw graph. This version is for UNIX base EXPRESS. To use at Windows base EXPRESS, please change byte order in the method dw.


This script convert X-ray IP data to a bitmap format file. The file ipf2bmp.rb is for Mac Science data format and the file img2bmp.rb is for RIGAKU data format. IP size should be corrected in the scripts, and please note that the bitmap format requires width with a multiple number of four.

Interface for Instrumemts


This script describes a class to use GP-IB board manifactured by National Instruments on Windows. Please declare as require 'gpib' at the begining of your script. To use a certain instrument, write for example as follows: g=Gpib::new; g.pad=adrs; g.sad=0; g.tmo=Gpib::T1s;; sleep 0.1; g.ibclr;. After that you can use several commands to control GP-IB board, by writing g.write("*CLS"), for instance. Commands which are often used is as follows.
padselect or read primary address
sadselect or read secondary address
write(string)send command string to present address
read(count)read from present address up to byte count
wait_rqswait for RQS(request to send)
Some more basic commands are supported. See this script in detail. On Linux, you can use linux-gpib.

prnprt95.rb, prnprtLX.rb

This script enables us to control parallel port on Windows. Please declare as require 'prnprt??' at the begining of your script. To send data to I/O port, this script uses iofunc32.dll which is contained in iochk32.lzh or diportio.dll, which is Port I/O Driver of DriverLINX. Please put this dll file at the same folder. The script prnprt95.rb is for Windows 95/98/Me, because this uses I/O port directry. You have to put dll file in the same directry as this script or system directry. Though the prnprtLX.rb is for Windows 95/98/Me/NT/2000/Xp, you have to install above mentioned I/O port driver. By using these script, you can control TTL signals, then many devices can be controled. For example I have succeeded to control stepping motor, which is used in a certain measuring system in our laboratory.


This script is for Linux to control parallel port. Before using this, you have to make node by typing 'mknod /dev/parport0 c 99 0', change group 'chgrp lp /dev/parport0', and add user to the group 'lp' by editting /etc/group, then change mode by 'chmod 660 /dev/parport0'.


By using printer port (parallel port), GPIB instruments can be controlled. This version only enables to "talk", namely, you cannot get information from instruments. However, there are many instruments which only accept setting data. Then I think it is useful because you do not need expensive interface board, only you have to do is making one cable. This script requires prnprt95.rb or prnprtLX.rb. The usage is almost same as gpib.rb which is described above. In this version, EOI line is neglected, and then binary data cannot be send. In detail, please read Japanese explanation.


By using printer port (parallel port), GPIB instruments can be controlled. This version enables to "talk" and "listen", then almost all instruments can be controled and you can also get information from instruments. To "listen", please confirm that your printer port support bidirectional data transfer. This script requires prnprt95.rb, or prnprtLX.rb. The usage is almost same as gpib.rb which is described above. To use this script, please make a cable with three transistor as described in the script. In this version, EOI line is used instead of DIO8 line. So, binary data cannot be send. To enable handshake to listen, two lines (IFC, REN) are replaced from talk only version. Then, InterfaceClear command cannot be send, and remote command is always sent. Though there are some restrictions, you can use most of functions which GPIB proposes. In detail, please read Japanese explanation.


This script defines a class to control Quantum Design PPMS (Physical Properties Measuring System) using GP-IB interface. By utilizing this, you can control temperature, magnetic field, and chamber operations.

Tools for Tk


This file difines a class to display graph using Tk. It can be controled by mouse, by dragging using right, left, or center button, and double-click. You can use this for simple graph.