Ruby and the State of Artificial Intelligence

Investigating the Tools and Resources for Artificial Intelligence and Machine Learning around the Ruby Ecosystem

Hello, fellow readers! It’s been a while since I showed up here, and today, we’ll talk about a hot topic: Artificial Intelligence (AI) and Machine Learning (ML) in the Ruby Ecosystem. We all are familiar with the recent boom in popularity of those themes. Everywhere, in every social media, you are bound to find not just news and content but many new products and systems that use these technologies in some capability. While languages like Python have long been considered a go-to, the community has also made strides in this field. In this article, we delve into the state of Artificial Intelligence and Machine Learning in this ecosystem, exploring the progress made, tools available, challenges faced, and the potential for the future.

(By the way, this may sound a little contentious, but most of the articles I saw floating around Google trying to approach this theme looked like they were written by Chat-GPT. Felt like mentioning this because I find it ironically funny.)

Now, before we delve any deeper, as a disclaimer, even though I’m pretty experienced as a rubyist, I do not have relevant experience in data science and advanced topics on Artificial Intelligence. While I do have more knowledge than the average Joe on the internet, my objective here is not to advise on implementing those kinds of systems but to try to evaluate Ruby’s place, as it stands, in this growing field. In the future, we’ll be following up on this article with some experiments and practical uses of the tools and resources mentioned here as I document my learning path on the core concepts involved in these technologies.

This article will be updated as time goes on to include more news, resources, and other relevant info as they surface on the web. Check the Changelog below for more information.

A blooming interest and a scarce past

Historically, Ruby has been recognized as a versatile and elegant language, majorly favored for web development and scripting tasks. With the burgeoning demand for AI and ML applications, the community has shown a renewed interest in incorporating these technologies into their projects. However, it seems we, as a community, may have lost the timing.

The focus Ruby received on web development in its early years may have outshined the community’s interest in developing high-quality tools for other kinds of applications. This has prevented us from having mature libraries and tools that support the development of such technologies, especially now that they are becoming more and more relevant. Since comparisons with Python will be unavoidable, let’s start here and take the SciPy library and its Ruby "equivalent" as an example, ScyRuby. From a quick glance in each official repository, it’s easy to see the difference in maturity just by looking at two things: Age and Activity.

Let’s start with age: while the SciPy repo has been rocking since 2001, the Ruby counterpart only got a repo to call home TEN YEARS later (2011). This means a world of difference in terms of maturity for any kind of tool. It’s ten years of development, optimizations, adjustments, and other vital contributions from the community. You cannot copy or offset this kind of growth and adoption by any means.

Not just that, but if we look now at the commits made to the repositories (contemplating the second metric, Activity), in 5 years of active development, SciRuby had only 264 commits to its name, the latest one being merged in 2016 (We can say pretty much confidence that this repository is not just unmaintained, but also kind of dead). Meanwhile, SciPy has a staggering 30,675 commits over more than 22 years of active development, the latest one being sent 1 hour ago (as of the time of this post’s writing).

Other tools you’d likely use in a Python environment, like "scikit-learn," have no solid counterpart in Ruby besides some unmaintained wrappers to the actual Python lib. Other libs, like NMatrix, have not been actively maintained for over five years and can only dream of having as many features and being as mature as Python’s widely adopted NumPy.

When researching to write this article, this dire situation of the scientific libraries in the Ecosystem struck me with a lot of sadness. As time went by, it seems the community decided that developing and maintaining these tools for Ruby was not worth it. I cannot stop from seeing this as a big wasted opportunity. As we’ll discuss a little further, Ruby as a language has many features that could be highly beneficial to the field. And not only that, but the community has been at the forefront of movements that incentivize developer happiness, the adoption of good practices, and the use of other conventions and tools that make codebases more maintainable and long-lived.

But I’m getting ahead of myself there. The point is that, historically, Ruby was never really associated with Artificial Intelligence, Machine Learning, or other science-related applications. Besides what we have already discussed in terms of the lack of a mature array of tools that would allow developers to quickly spin up their projects caused by the "overfocus" on web development, the root of this dissociation may also be related to two more factors we have not mentioned yet: Ruby’s historical low performance, and how good it is in transferring responsibility. I will only breeze over those two topics as the one we discussed above is way more relevant in understanding where we stand.

So, first. Ruby’s performance. While it’s true that Ruby was historically lagging behind in terms of speed and performance, we can say that this is not the case anymore. Some recent improvements in version 3.0 make it just as fast as Python (and actually faster in some tasks). If you are interested in some benchmarks, I included a link in the references section below. Even though it had an enormous gain in performance over the last years (3.0 claims to be 3x faster than 2.0; more details on how these claims aged in the references below), one thing tends to linger: reputation. It’s not speed (at least not anymore) preventing the adoption in fields where high performance is essential but the perception that it is slow. Currently, we can be confident that where Python is used for its performance, Ruby can also be used.

Secondly, about Ruby’s ability to transfer responsibility. Many mature libraries in the ecosystem allow you to run or call code implemented in another language using its native runtimes. Since we are talking about this topic, two relevant ones to note are PyCall and FFI, each allowing you to call Python and C code from Ruby, respectively. In that sense, it’s easy to understand the low interest of the community in implementing some tools directly in Ruby. Why would you rewrite all of the features implemented by SciPy, NumPy, or scikit-learn when you can call them from your Ruby code? That way, you can leverage those libs’ maturity alongside Ruby’s features. This is a very interesting topic, and we’ll explore it later.

So, what about my smart project using Ruby?

With all of that said, It may sound like I’m a bit pessimistic about the prospects of these technologies in the ecosystem, but the truth could not be further from that. The maturity of the community as a whole, as well as some of Ruby’s core features and conventions, can be significant assets in our favor in these days of high demand for AI and ML applications. Let’s talk about a few of those features:

Flexibility and Expressiveness

This flexibility allows developers to experiment and prototype quickly, a crucial aspect of ML and AI development. Its dynamic nature enables rapid iteration, making it easier to adapt algorithms and models as insights evolve. This flexibility can be invaluable in the early stages of experimentation, enabling developers to explore and rapidly implement novel ideas.

Metaprogramming for the win!

I rarely see anyone talking about this feature, but as I dive deeper into playing with Artificial Intelligence and Ruby, I become more and more interested in playing with it. These metaprogramming capabilities allow developers to write code that, in a way, also writes code. Metaprogramming can be used to create dynamic methods that automate data preprocessing and transformation, create base classes or templates that can be extended or modified on the fly, help with hyperparameter tuning, and probably many other aspects.

Integration with Web and Applications

There’s no escape from talking about this subject. We all know Ruby’s strength in web development, and using this platform to quickly make your ML and AI available can be highly valuable from a product perspective. Frameworks like Rails, Hanami, and Sinatra can quickly incorporate AI/ML-driven features into web applications. Not just that, but what I quoted a few paragraphs ago as one of the reasons Science libraries may not have evolved as much in the ecosystem can be seen as a great ally here: How easy it is to transfer responsibility.

If you want to use some other tool from a language with more mature libraries like Python, you can! While still leveraging the rich web development environment and community Ruby has to offer. Also, while not a universal truth, at least from my experience, Ruby codebases for web applications tend to be more maintainable and long-lived and have lower onboarding times for new developers.

Of course, I would be naive not to admit that, from a business perspective, splitting your product into different technologies has its risks, especially in the beginning. However, in this case, we cannot hide the benefits either. In the right circumstances, it can be a win-win situation. The best of all worlds!

A new hope

Despite the current scenario, the community has acknowledged these limitations as it has actively been working on creating and enhancing science-related libraries, contributing to open-source projects, and building resources to support others on their journey. As the engagement grows, more comprehensive libraries, optimized tools, and educational resources become more accessible, attracting more developers and companies to use Ruby in their AI and ML products. Currently, specific use cases can leverage its strengths that align with requirements for applications like Chatbots, data analysis pipelines, and other types of web applications that can significantly benefit from many of its features.

Regarding tools, I’d like to highlight a few gems that have been making waves and renewing our hopes that Ruby may become more relevant in the field. Some of those gems are new, some have been "rediscovered", but all look promising, and a few have been growing rapidly, raising a lot of interest.

Torch.rb

Torch.rb is a deep learning library for Ruby, powered by LibTorch (PyTorch). It has a series of tools for NPL, recommendation systems, audio tasks, data loading, and much more. You can also find some tutorials, examples, and a comprehensive 60-minute getting started guide in the repo. It has been around since 2019 and is actively being developed.

Rumale

Inspired by Python’s Scikit-Learn, Rumale supports a wide array of machine-learning algorithms and integrates nicely with Torch.rb (through another gem), and supports parallel processing. It has been kicking since 2017, and the development is very active. The documentation is also quite reasonable. To me, this is one of the most promising projects, and I will definitely keep an eye out for further developments.

Eps

Eps is a very similar gem to the Rumale one above, but it’s simpler to use, and it has a feature that integrates models created in R or Python that can be served in Ruby. Eps’ development has picked up pace again a few months ago, even if it has not been that much active.

xLearn Ruby

xLearn is a gem for machine learning that currently supports Linear models, Factorization machines, and Field-aware factorization machines. It has also been around since 2019, but its development does not seem to be quite active (eight months since the last commit, as of the time of writing)

RubyFann

This gem binds to the Fast Artificial Neural Network (FANN) library and makes its features available within the Ruby environment. It’s not a new library by any means (it has been around since 2013), but it’s a good bet to deal with Neural Nets as of today in Ruby. The development seems to have been stalled a few years ago, but there has been some movement recently in the repository, so it may be good to keep an eye on it.

ruby-dnn

ruby-dnn is also a deep learning gem that supports full connected neural networks, convolution neural networks, and recurrent neural networks. It has some cool projects implemented with it (like this one: https://github.com/unagiootoro/ruby-rl), and it has been steadily developed over the last year.

LangChain.rb

This is one I’m very excited to try out myself! The goal of this project is to "abstract complexity and difficult concepts to make building AI/ML-supercharged applications approachable for traditional software engineers". It provides an abstraction layer on top of a LOT of tools and libraries and nests them neatly in composable structures that can be very intuitive to use. Currently under fast-paced development, I’d not recommend it for production-ready features, but this gem’s future sure looks interesting (insert joke about lapidating gems here).

Conclusion

The state of Artificial Intelligence and Machine Learning in the Community is one of growing interest and a willingness to explore new frontiers beyond traditional web development. While challenges exist to overcome, including the scarcity of specialized libraries and the concerned memory of performance issues, the community’s determination to innovate and collaborate suggests a promising future for Ruby in the Artificial Intelligence landscape.

As Ruby developers continue to experiment, create, and refine relevant tools and libraries, the possibilities for Ruby applications powered by Artificial Intelligence are bound to expand. By embracing the field, the community can position itself at the intersection of creativity, innovation, and technology, contributing to the broader evolution of Artificial Intelligence.


Links and References

Links for the mentioned gems:

Torch.rb – https://github.com/ankane/torch.rb
Rumale – https://github.com/yoshoku/rumale
Eps – https://github.com/ankane/eps
XLearn – https://github.com/ankane/xlearn-ruby
ruby-dnn – https://github.com/unagiootoro/ruby-dnn
langchainrb – https://github.com/andreibondarev/langchainrb

Wrappers for third-party resources:

OpenAPI (Chat-GPT, DALL-E, etc) – https://github.com/alexrudall/ruby-openai
Replicate – https://github.com/dreamingtulpa/replicate-rails and https://github.com/dreamingtulpa/replicate-ruby
RubyFann – https://github.com/tangledpath/ruby-fann

Awesome Ruby for Artificial Intelligence:

Machine Learning: https://github.com/arbox/machine-learning-with-ruby
Natural Language Processing: https://github.com/arbox/nlp-with-ruby
Data Science: https://github.com/arbox/data-science-with-ruby

Calling Non-Ruby code:

FFI – https://github.com/ffi/ffi
PyCall – https://github.com/mrkn/pycall.rb
ExecJS – https://github.com/rails/execjs
How To Write A Ruby C Extension (Step-By-Step) – https://www.rubyguides.com/2018/03/write-ruby-c-extension/
Using the scikit-learn machine learning library in Ruby using PyCall – https://www.practicalai.io/using-scikit-learn-machine-learning-library-in-ruby-using-pycall/

About Performance:

Why Wasn’t Ruby 3 Faster? – https://www.fastruby.io/blog/ruby/performance/why-wasnt-ruby-3-faster.html
Is Ruby 3 Actually Three Times Faster? – https://codefol.io/posts/is-ruby-3-actually-three-times-faster/
Benchmarking Ruby 2.5 to 3.1 – https://gettalong.org/blog/2021/benchmarking-rubies.html

Comparing Python 3 and Ruby 3.0:

https://benchmarksgame-team.pages.debian.net/benchmarksgame/fastest/ruby-python3.html

Some recent Videos and Tutorials on Artificial Intelligence:

Easy machine learning with Ruby using Rumale – https://dev.to/kojix2/easy-machine-learning-with-ruby-using-svmkit-4n86
Implementing Machine Learning in Ruby – https://reintech.io/blog/implementing-machine-learning-in-ruby
Integrate Replicate in Rails Application – https://blog.saeloun.com/2023/07/28/integrate-replicate-ai-in-rails-application/
Adding Machine Learning Features To Your Ruby On Rails Apps – https://medium.com/mlearning-ai/adding-machine-learning-features-to-your-ruby-on-rails-apps-790be219a66f
AI & Ruby: an introduction to neural networks – https://dev.to/leandronsp/ai-ruby-an-introduction-to-neural-networks-23f3
Forecasting the Future: Intro to Machine Learning in Ruby – https://www.youtube.com/watch?v=nO8vY1iNlgQ
Understanding How ChatGPT Works – Running your own AI – https://www.youtube.com/watch?v=O68y0yRZL1Y [Brazilian Portuguese, translated captions works very nicely]

Older Videos and Tutorials on Artificial Intelligence:

Practical Machine Learning in Ruby – https://www.youtube.com/watch?v=Zl17mtp1adM
SciRuby Machine Learning Current Status and Future / Kenta Murata – https://www.youtube.com/watch?v=gfQ8XEy7vO4 [Japanese, automatic captions are good]

Other articles about using Ruby for Artificial Intelligence and Machine Learning:

Why Ruby on Rails Works For Machine Learning Development – https://blog.airbrake.io/why-ruby-on-rails-works-for-machine-learning-development
Empowering Ruby in the World of Machine Learning – https://dev.to/rpaweb/empowering-ruby-in-the-world-of-machine-learning-337p
Exploring Genetic Algorithms with Ruby – https://dev.to/rpaweb/exploring-genetic-algorithms-with-ruby-4lae
15 More ML Gems for Ruby – https://ankane.org/more-ml-gems
16 New ML Gems for Ruby – https://ankane.org/new-ml-gems
Real World Example of Machine Learning on Rails – https://serpapi.com/blog/real-world-example-of-machine-learning-on-rails/
Rails, Meet Data Science – https://ankane.org/rails-meet-data-science

Other links:

PyTorch – https://pytorch.org/
Top 9 Programming Languages For Artificial Intelligence – https://www.bairesdev.com/blog/top-languages-for-ai/


Changelog

  • October 17th, 2023
    • Added Reference to Resource: Top 9 Programming Languages For Artificial Intelligence
  • September 6th, 2023
    • First Version

We want to work with you. Check out our "What We Do" section!