Allows extending ruby's syntax and compilation process.
Add this line to your application's Gemfile:
And then execute:
Or install it yourself as:
$ gem install vernacular
At the very beginning of your script or application, require
vernacular. Then, configure your list of modifiers so that
vernacular knows how to modify your code before it is compiled.
. do |config| pattern = /~n\(([\d\s+-\/*\(\)]+?)\)/ modifier = ::.(pattern) do |match| eval(match[3..-2]) end config.add(modifier) end
will extend Ruby syntax to allow
~n(...) symbols which will evaluate the interior expression as one number. This reduces the number of objects and instructions allocated for a given segment of Ruby, which can improve performance and memory.
Modifiers allow you to modify the source of the Ruby code before it is compiled by injecting themselves into the require chain through
RubyVM::InstructionSequence::load_iseq. They can be any of the preconfigured modifiers built into
Vernacular, or they can just be a plain ruby class that responds to the method
source is a string of code. The method should returned the modified source.
Regex modifiers take the same arguments as
String#gsub. Either configure them with a string, as in:
or configure them using a block, as in:
::.(pattern) do |match| eval(match[3..-2]) end
For access to and documentation on the
ASTModifier, check out the
After checking out the repo, run
bin/setup to install dependencies. Then, run
rake test to run the tests. You can also run
bin/console for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run
bundle exec rake install. To release a new version, update the version number in
version.rb, and then run
bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the
.gem file to rubygems.org.
Bug reports and pull requests are welcome on GitHub at https://github.com/kddeisz/vernacular.
The gem is available as open source under the terms of the MIT License.