All Projects → Anyolite → anyolite

Anyolite / anyolite

Licence: MIT license
Embedded mruby/Ruby for Crystal

Programming Languages

crystal
512 projects
c
50402 projects - #5 most used programming language
ruby
36898 projects - #4 most used programming language

Labels

Projects that are alternatives of or similar to anyolite

dragonblocks
Blockly for DragonRuby!
Stars: ✭ 20 (-83.33%)
Mutual labels:  mruby
Ruby Install
Installs Ruby, JRuby, Rubinius, TruffleRuby or MRuby
Stars: ✭ 1,651 (+1275.83%)
Mutual labels:  mruby
Rvm
Ruby enVironment Manager (RVM)
Stars: ✭ 4,700 (+3816.67%)
Mutual labels:  mruby
mruby-hiredis
mruby bindings for https://github.com/redis/hiredis
Stars: ✭ 12 (-90%)
Mutual labels:  mruby
mruby-tflite
MRuby binding for TensorFlow Lite
Stars: ✭ 14 (-88.33%)
Mutual labels:  mruby
mruby-esp32
mruby application template for ESP32
Stars: ✭ 81 (-32.5%)
Mutual labels:  mruby

Anyolite

Anyolite is a Crystal shard which adds a fully functional mruby (or even regular Ruby) interpreter to Crystal.

Test

Release ReleaseDate

License

Description

Anyolite allows for wrapping Crystal classes and functions into Ruby with little effort. This way, Ruby can be used as a scripting language to Crystal projects, with the major advantage of a similar syntax.

Useful links for an overview:

Features

  • Bindings to an mruby interpreter
  • Near complete support to regular Ruby as alternative implementation (also known as MRI or CRuby)
  • Wrapping of nearly arbitrary Crystal classes and methods to Ruby
  • Easy syntax without unnecessary boilerplate code
  • Simple system to prevent garbage collector conflicts
  • Support for keyword arguments and default values
  • Objects, arrays, hashes, structs, enums and unions as function arguments and return values are completely valid
  • Ruby methods can be called at runtime as long as all their possible return value types are known
  • Ruby closures can be handled as regular variables
  • Methods and constants can be excluded, modified or renamed with annotations
  • Options to compile scripts directly into the executable

Prerequisites

You need to have the following programs installed (and in your PATH variable, if you are on Windows):

  • Ruby (for building mruby)
  • Rake (for building the whole project)
  • Git (for downloading mruby)
  • GCC or Microsoft Visual Studio 19 (for building the object files required for Anyolite, depending on your OS)

Using regular Ruby instead of mruby

It is possible to use Anyolite with regular Ruby (MRI) instead of mruby. An instruction to install MRI can be found at Using Ruby instead of mruby in the wiki.

Installing

Put this shard as a requirement into your shard.yml project file and then call

shards install

from a terminal.

Alternatively, you can clone this repository into the lib folder of your project and run

rake build_shard

manually from a terminal or the MSVC Developer Console (on Windows) to install the shard without using the crystal shards program.

If you want to use other options for Anyolite, visit Changing build configurations in the wiki.

How to use

Imagine a Crystal class for a really bad RPG:

module RPGTest
  class Entity
    property hp : Int32

    def initialize(@hp : Int32)
    end

    def damage(diff : Int32)
      @hp -= diff
    end

    def yell(sound : String, loud : Bool = false)
      if loud
        puts "Entity yelled: #{sound.upcase}"
      else
        puts "Entity yelled: #{sound}"
      end
    end

    def absorb_hp_from(other : Entity)
      @hp += other.hp
      other.hp = 0
    end
  end
end

Now, you want to wrap this class in Ruby. All you need to do is to execute the following code in Crystal (current commit; see documentation page for the version of the latest release):

require "anyolite"

Anyolite::RbInterpreter.create do |rb|
  Anyolite.wrap(rb, RPGTest)

  rb.load_script_from_file("examples/hp_example.rb")
end

Well, that's it already. The last line in the block calls the following example script:

a = RPGTest::Entity.new(hp: 20)
a.damage(diff: 13)
puts a.hp

b = RPGTest::Entity.new(hp: 10)
a.absorb_hp_from(other: b)
puts a.hp
puts b.hp
b.yell(sound: 'Ouch, you stole my HP!', loud: true)
a.yell(sound: 'Well, take better care of your public attributes!')

The example above gives a good overview over the things you can already do with Anyolite. More features will be added in the future.

Limitations

See Limitations and solutions in the Wiki section for a detailed list.

Why this name?

https://en.wikipedia.org/wiki/Anyolite

In short, it is a rare variant of the crystalline mineral called zoisite, with ruby and other crystal shards (of pargasite) embedded.

The term 'anyoli' means 'green' in the Maasai language, thus naming 'anyolite'.

Roadmap

Upcoming releases

Version 1.1.0

Features

  • Added direct bindings to Regex from Crystal (Regexp in Ruby)
  • Added option to use a separate Ruby interpreter
  • Added safeguard to catch non-fatal Crystal exceptions and raise them as Ruby errors

Usability

  • Updated mruby config file to use libucrt instead of msvcrt
  • Discarded old and problematic Regex mruby gem

Bugfixes

  • Fixed error when running shards install on Windows
  • Fixed compilation warning messages for Windows
  • Fixed problems with Regexes due to PCRE conflicts
  • Fixed problems with Anyolite on Crystal 1.5.1
  • Unspecified arguments now always correctly throw warnings instead of confusing errors
  • Fixed compiletime error when casting to Char

Later releases

  • Automated generation of Ruby documentations for wrapped functions
  • MRI support on Windows (does currently not work for some reason)
  • Mac support and continuous integration

Wishlist, entries might not be possible to implement

  • Splat argument and/or arbitrary keyword passing
  • Support for slices and bytes
  • Classes as argument type
  • Resolve context even in generic type union arguments
  • General improvement of type resolution
  • Bignum support
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].