Shipping programs with OCRAN
Your program works. Now Ida should get it – she has Windows, but no Ruby, and doesn't want to install it. OCRAN (One-Click Ruby Application Next) packs your script, the Ruby interpreter and every gem it needs into one single .exe. Ida double-clicks it, and it runs.
The building happens on your computer, not in the browser: OCRAN packs the Ruby installed there. For the package to work, though, your program has to know a few things – and those we can practise here. Our program: Chunky's diner, with the menu in a text file.
File.write("menu.txt", "Bacon: 2.50\nEgg: 1.20\nCoffee: 3.00\n")
def menu(file)
File.read(file).lines.to_h do |line|
name, price = line.split(":")
[name.strip, price.to_f]
end
end
menu("menu.txt")The menu is a Hash. Now the main program – and a line you find in almost every OCRAN program:
def main
dishes = menu("menu.txt")
puts "Welcome to Chunky's diner!"
dishes.each { |name, price| puts format("%-8s $%5.2f", name, price) }
puts "Enjoy your meal!"
end
main unless defined?(Ocran)Why unless defined?(Ocran)? To find out which files and gems your program needs, OCRAN starts it once while building and watches what it loads. During this dry run the constant Ocran exists. So your program loads everything it needs – the requires run – but the main program, which might open a window or wait for input, stays quiet. This is what the dry run looks like:
module Ocran; end # what OCRAN does while it builds
main unless defined?(Ocran) # -> nothing happens
Object.send(:remove_const, :Ocran) # and gone again
defined?(Ocran)Nothing printed – just right. Here in the browser there is no Ocran, which is why the main program ran one cell up.
The second trap is files. An .exe from OCRAN is an archive that unpacks itself: when it starts, it puts your script in a temporary folder and runs it there. So __dir__ points into that folder, which is deleted again when the program ends – not to where the .exe is. Where it is, ENV["OCRAN_EXECUTABLE"] says:
def app_folder
if ENV["OCRAN_EXECUTABLE"]
File.dirname(ENV["OCRAN_EXECUTABLE"])
else
__dir__
end
end
app_folderIn the browser there is no .exe, so the method takes __dir__, the script's folder: ".". Let's play the .exe – OCRAN sets the environment variable when it starts, we set it by hand and delete it afterwards. (Ruby on Windows understands / just as well as \.)
ENV["OCRAN_EXECUTABLE"] = "C:/Users/Ida/Desktop/BaconCalc.exe" # what it looks like in the .exe
puts app_folder
puts File.join(app_folder, "menu.txt")
ENV.delete("OCRAN_EXECUTABLE")So: whatever Ida should change or keep – the menu, saved receipts – goes next to the .exe and is found through OCRAN_EXECUTABLE. What only your program needs, pictures for example, you give OCRAN when building; at the start it lies next to the script, under __dir__.
All together this makes bacon.rb. At the end it waits for Enter – otherwise the console window closes right after the double-click, before Ida can read anything. Below the cell you can download both files:
program = <<~'PROGRAM'
# bacon.rb – Chunky's diner, to go
def app_folder
if ENV["OCRAN_EXECUTABLE"]
File.dirname(ENV["OCRAN_EXECUTABLE"])
else
__dir__
end
end
def menu(file)
File.read(file).lines.to_h do |line|
name, price = line.split(":")
[name.strip, price.to_f]
end
end
def main
dishes = menu(File.join(app_folder, "menu.txt"))
puts "Welcome to Chunky's diner!"
dishes.each { |name, price| puts format("%-8s $%5.2f", name, price) }
puts "Enjoy your meal!"
puts
puts "Press Enter to quit."
$stdin.gets
end
main unless defined?(Ocran)
PROGRAM
File.write("bacon.rb", program)
File.write("menu.txt", "Bacon: 2.50\nEgg: 1.20\nCoffee: 3.00\n")
program.lines.sizeLook at the last line once more: without unless defined?(Ocran), the dry run would wait for Enter while building – and OCRAN with it.
On Windows with RubyInstaller (with the DevKit), in the folder with both files:
gem install ocran ocran bacon.rb --output BaconCalc.exe
Then put BaconCalc.exe and menu.txt in a folder and send it to Ida. Useful options: --icon fox.ico for an icon of your own, --windows for programs with a window and no console (or name the file .rbw), --gemfile Gemfile for projects with Bundler, --chdir-exe-dir so that relative paths start in the .exe's folder. More files go on the end: ocran bacon.rb images/*.png. OCRAN is not a cross-compiler: you build an .exe on Windows, a program for Linux or macOS there. Experimentally it can even put out programs as WebAssembly (--wasm) – the way this course runs Ruby in the browser. OCRAN comes under the MIT licence.
receipt.txt, and Ida should find it again – next to the .exe, not in the temporary folder. Write the method receipt_path: it returns the path of receipt.txt next to the .exe when the program runs as an .exe, and next to the script otherwise.def receipt_path
# next to the .exe when there is one - next to the script otherwise
end