Ruby Application with metaprogramming - How to get bytecode or binary or something which can be ported - ruby

I have a ruby application with meta programming. Application job is to read multiple input files (which can have user defined functions and data). Application parses and then executes functions after parsing.
I am wondering, is there a way I can get an executable or bytecode or something after parsing is done. So I can export this exe/bytecode and run "execute" part of processing on a different machine(same configuration).
Env detail:
Ruby 2.7.2
OS: Red Hat Enterprise Linux (7.7)

Not really. Because of how dynamic Ruby is, the only way to meaningfully know that you have valid code is to run it. The C API almost supports this through the ruby_exec_node function, but last I checked there is not much support for this style of running Ruby. Even if that was more convenient to use, I don't know how much skipping the parsing step would really save you.
Most of the Ruby VMs that produce bytecode are JIT-style, meaning they only generate byte code after they have executed the code once via the VM. This works best for Ruby because again it is so dynamic that it's hard to say what it is going to do without actually running it.

Related

Does the Ruby VM define the file format that runs on it?

Like the ".class" file that runs on JVM, does the Ruby VM(MRI, or YARV) define the file format that rns on it?
I have read in some articles that YARV bytecode was considered a internal format, which means that there are little documentations or specifications about it, is it true?
Thanks!
tl;dr No, ruby does not define a runable format other than the language itself.
It is true that ruby doesn't have a standard bytecode format like java, etherium, and others. Every ruby implementation JRuby, MRI Ruby, TruffleRuby, etc. gets to decide how exactly they run ruby code.
Ruby implementations can choose to implement their own bytecode format. Those formats would likely be private to that ruby implementation and unlikely to be seen outside of that eco-system. (however as we've seen with languages popping up that run on the JVM it's possible for other people to piggy back off your language's VM if they'd like)
It's completely valid for a ruby implementation to run inside the JVM run on java byte code JVM jruby, to introduce a compilation step to produce native machine code ruby-llvm, or even to sometimes produce native machine code at runtime with a JIT and sometimes use a more traditional interpreter MRI Ruby.

Is there a way to build CLI with no dependencies required?

Recently I though about scaffolding a little CLI with Ruby, but was concerned about using it on a machine with no Ruby installed. I've searched for examples of popular CLI's and found that Docker CLI is built with Go language. I'm able to use this CLI on my computer with no Go installed. How can one build a tool that will not require you to install Ruby?
My guess is that there's a build process involved and it might be compiled to something present on most systems, like shell or smth. Sorry if this is a lame guess/question!
(note: this is not a detailed answer, just a summary of how it works)
Cli programs are just as other programs, there is nothing special about them.
Go is a compiled language - a program called "compiler" takes the go code and translate it directly to machine language, following the conventions imposed by an operative system. It becames pure 0 and 1, no references to anything else. The main advantage is that is self-contained, but you have to recompile it on every different architecture (32bit, arm processors, ...) and operating system (windows, linux, macos) - it's the operating system that take cares of redirecting input and output on cli.
Ruby, instead, is interpreted. There is a program called "Ruby interpreter" which translates your code to the appropriate machine code on the fly. It's a different approach, it's more "high level".
The advantage is that you don't need to recompile the code. However, the "Ruby interpreter" itself must be written in some compiled language.

how to use ruby code after its written, is it standalone or need to be in a web application?

Confused a little on ruby. I know it makes .rb files, but does it make exe or com files or is it just used as as web application?
I know a bit writing the code, but what to do with the files after.
the question is a bit too broad.
you have to step back and look at how source code in general ends up being executed (i.e. it is used).
In case of some programming languages (e.g. C/C++) it's compiled to a native form and can be executed directly afterwards;
In case of other languages it's compiled to an intermediate form (e.g. Java/C#) and executed by a vm (jvm/clr)
In case of yet other languages is interpreted at runtime (e.g. Ruby/Python).
So in the specific case of Ruby, you have the interpreter that loads the rb files and runs them. This can be in the context of standalone apps or in th e context of a web server, but you almost always have the interpreter making sense of the ruby files. you don't get an executable the same way as the you get for languages that are compiled to machine code.
Normally you just run the .rb file in the shell or command prompt. For example in the windows command prompt:
C:\path\> ruby filename.rb
This will execute the filename.rb file from the command prompt.
If you need to run a ruby program on a computer without a ruby installation there are a few options out there.
Try this website:
https://www.ruby-toolbox.com/categories/packaging_to_executables
I personally have used OCRA to pass a program to relatives who are less computer literate. It was pretty straight forward,I haven't tried the other tools.
Good luck,

Compile ruby script for faster use

I have a ruby script of around 2200 lines which is being used repeatedly, so is there a way to convert it into binary or compile it so it runs faster.
It seems that only JRuby has a compiler which is good news if Java is your target platform and no help if not.
Perhaps you could re-architect your solution to include the Ruby interpreter in a pipeline so that your script can be launched once and run continually as it receives input?
If you are using MRI, your best bet is to optimize your code as the JIT compilation already delivers proven performance. You can also switch to 1.9 version as it is faster in various cases.

Scripting language that can produce a small, independent, Windows EXE?

I'd like to do some light data processing - a little binary data manipulation followed by conversion to text serialization. The result is written to a file, and processed by an external program (run by my program). The data processing is more than I'd care to consider doing in batch files.
I'd prefer to use a scripting language, but not have to install the language first. The target computers are mostly older Windows boxes, which are disconnected from the network (no updates, such as PowerShell)
I'm not familiar with the various language's tools for creating EXE files. Which ones have solutions that work well and don't produce huge files? (i.e., whole interpreter package plus my script.)
For my money (its free) AutoIt 3 is exactly what your looking for. AutoIt produces relatively (250k is the standard overhead) small stand alone exes. It has a full perl like regex engine so your light data processing should be a breeze (I've written some pretty heavy data processing scripts in it myself). When downloading autoit be sure to get the full version including Scite this makes compile to exe a one click operation.
I know I might get flamed for this, but VB 6 is a viable option. Since XP SP2 (I think, possibly earlier), Windows has come with its runtimes installed. Not sure about vista.
Theres also the Windows Scripting Host that uses VBScript and JScript.
http://en.wikipedia.org/wiki/Windows_Script_Host
Lua is an excellent choice for that kind of stuff. You can integrate it in your executable or use the standalone Lua interpreter to run your scripts.
While waiting for answers I ran across Shoes, which can make Ruby .exe (I'm most familiar with Ruby) I got it mostly working, although the size of 2.4MB was a bit larger than I'd like. However, I found that it would crash when changing application focus.
I switched to a 'regular' terminal script, and found rubyscript2exe, which, after working around a problem with rubygems, seems to work, and creates a ~700kb file.
I did rather like some of the options presented, but it's not worth redeveloping at this point.
Python with py2exe. Depends on what you mean by small though.
Would using PowerShell script be something you've considered. The data processing might be richer there.
Why not knock up a .NET application? There are free editions of the IDE, and the Framework comes with Windows as a standard component (which also includes a C# compiler, as it happens.)

Resources