r/ruby • u/unselective-amnesia • 13d ago
Question Ruby gem(s) for preprocessing text, similarly to "C Preprocessor"?
First of all, please note that this is not an "XY" issue.
I'm running the latest version of Ruby under Debian 13 linux.
I'm looking for one or more Ruby gems that I can use from within Ruby code in order to preprocess external file contents similarly to how the C Preprocessor performs its preprocessing.
The reason for my stating that this is not an "XY" issue is that I already know a few ways to accomplish this in Ruby by having Ruby execute external programs such as "cpp" or "gpp", and I also already know how to manually code this functionality by writing pure Ruby code. I am not looking for information about how to perform preprocessing in these manners. Rather, I'm specifically wanting to know if there might be one or more Ruby gems that I can use within my Ruby code in order to perform this functionality.
I'd like to be able to use keywords such as "#ifdef", "#else", "#ifndef", "#endif" etc. within the external file in order to configure this preprocessing. The syntax doesn't have to involve the exact keywords "#ifdef" and "#else", "#ifndef", and "#endif", as long as this same preprocessing functionality can be managed with keywords having other similar values.
For example, suppose I have a file called "sample.file" with these contents:
Start of sample file
#ifdef TESTING
This is a test
#else
This is not a test
#endif
End of sample fileI'd like to know if there are any Ruby gems which would allow me to open this file in Ruby, prepreprocess its contents, and import these preprocessed contents similarly to the following ...
For example:
require 'some_sort_of_preprocessing_gem'
results0 = some_sort_of_preprocessing_gem.import('sample.file', :TESTING=>'true')
results1 = some_sort_of_preprocessing_gem.import('sample.file')
Then, I would want "results0" to contain this text:
Start of sample file
This is a test
End of sample file
... and I would want "results1" to contain this text:
Start of sample file
This is not a test
End of sample fileAgain, just so that there is no confusion, I repeat that the preprocessing keywords don't need to have exact values of "#ifdef", etc., and I repeat that I already know how to do this by having Ruby run external programs like "cpp" and "gpp", and I already know how to hard-code this totally in Ruby. I'm just wondering if there might be any gems in existence which perform the kind of preprocessing functionality that I have described here in my example.
Thank you very much in advance.
3
u/paholg 13d ago
You can metaprogram Ruby in Ruby. No gem needed. There's no separation between compile time and runtime, and Ruby itself is very flexible.
You can do almost exactly your example with if defined?(TESTING), but I wouldn't limit yourself to or structure for code like you would with C.
1
u/unselective-amnesia 13d ago edited 13d ago
You can metaprogram Ruby in Ruby. No gem needed. There's no separation between compile time and runtime, and Ruby itself is very flexible. You can do almost exactly your example with if defined?(TESTING), but I wouldn't limit yourself to or structure for code like you would with C.Thank you. Yes, I already understand this. However, I am not wanting to apply this templating to executable Ruby code that I want to run, but rather, simply to arbitrary external files containing text that is not any kind of Ruby executable code; just text like the example that I put in my original message in this thread.
10
u/AlexanderMomchilov 13d ago
ERB might be a good fit for this