Ruby Machine Learning: Ruby on Rails – Yay or Nay? (Updated)

It’s rapidly gaining in popularity – it’s common in modern web applications as well as in services such as Netflix, Spotify, Amazon and Facebook. Machine Learning is a good solution for apps based on recommendations or some kind of predictions. If you want to build this kind of app you will need an efficient backend technology to support it – is Ruby on Rails the right choice? The current status of Ruby on Rails in the machine learning landscape shows that while it is not the primary framework for advanced AI, it is increasingly being used to integrate machine learning features into web applications.
What is Machine Learning?
The most famous definition of Machine Learning is ‘the subfield of computer science giving computers the ability to learn without being explicitly programmed.’
In fact, this gives us a nice idea of what it is all about.
Machine Learning is a part of data science – it is used when we want to use computers to predict unknown results based on related bulky data sources. It’s a good way to discover any kind of uncertainty, such as recommendations, predictions or detections of described situations. Machine learning is actually a subset of artificial intelligence, which encompasses a broader range of technologies and methods for enabling computers to perform tasks that typically require human intelligence.
We don’t need to plan and implement any algorithms – because the computer gains the ability to be smart and learn new things. Practical machine learning is essential for applying these concepts to real-world problems and building effective solutions.
How Does It Work?
With Machine Learning you don’t need to specifically instruct a computer on what to do – if you want to be smart and predict uncertain values applications, use specific structures and tools e.g. neural nets.
These technologies learn new facts and make predictions in a way which is similar to that of the human brain. For example, neural nets are composed of layers of units called neurons – we see a clear analogy of how it could work. Neural networks can take various forms, including deep neural networks and recurrent neural networks, which are used for complex tasks like sequence modeling. Neural network training involves adjusting the connections between neurons to improve prediction accuracy.
How can I get started?
As Machine Learning is a part of Data Science it is a composition of various mathematical computations. Linear algebra operations are fundamental to many ML tasks, and libraries like the GNU Scientific Library can assist with these computations. It means that an application using the technology needs to provide complex calculations, fast.
Since it’s not a trivial software problem you need to ensure the best tools are chosen. Other tools are often used alongside Ruby to handle complex mathematical operations in machine learning.
Is Ruby on Rails a Good Choice for Machine Learning?
Ruby is an elegant programming language which found its role in web development and scripts. The ruby ecosystem, including its libraries and community support, plays a growing role in supporting machine learning and AI development.
With the help of the Ruby and Rails framework developers can build MVPs in a way which is both fast and stable – this is thanks to the availability of various packages called gems, which help solve diverse problems quickly (creating a GEM - a step by step tutorial).
When looking at Machine Learning gems we can conclude the choice is not that rich – the problem is that the described solutions are not documented enough.
They also do not provide efficient computation speed and only have a small community of developers. These factors show that there are more risks than advantages when using Ruby gems as Machine Learning solutions, making it not the most suited choice. However, Ruby's ability to interface with C and Python libraries empowers Ruby for advanced machine learning tasks, compensating for some of its limitations.
Moreover, tools and packages are as useful as the language of development. Ruby is definitely one of the most interesting programming languages. It has many proved purposes, but fast computing is not one of them. Ruby does not match Machine Learning, and we need to look into something better. Still, open source projects have been developed to enhance the Ruby programming language for machine learning, making it a field worth exploring for developers interested in empowering Ruby beyond web development.
What's the Alternative?
Python is also a popular programming language which is often used in Data Science projects because:
-
It has numerous packages for Machine Learning and other computations. The prime examples are numpy, pandas, keras, tensorflow, scikit learn. These packages are well-documented, which is helpful in starting with new projects and solutions – it also speeds up the process of fixing bugs.
-
Its libraries are simply powerful. It means that they comprise many features helpful in complex computations. The development is fast, efficient and stable. It is also common that they use a range of computation speed improvements – all of these advantages make these tools mature and reliable.
-
Another important advantage of using Python libraries is considerable support from the community as the developers can easily find tutorials and tips valuable in a development process. A stable community makes the start threshold lower – it is easier to use new technologies from scratch.
-
Python is a developer-friendly language which is easier to start with for Ruby on Rails developers compared to other, lower-level programming languages. The syntax is intuitive, and it parallels the one in Ruby more than other popular languages.
Python's interoperability with other languages, such as C and Ruby, further enhances its capabilities for machine learning by allowing developers to leverage resources and tools from other languages.
If you want to learn more about Python our experienced team have put together some great information for you.
Tensorflow
We need to choose the best Python library for Machine Learning purposes – we recommend using Tensorflow, a popular and powerful tool from Google. Tensorflow is both a deep learning library and a deep learning framework, widely used for building and training neural networks in various AI applications.
It provides stable implementation for Python, C++ and many other programming languages. We decided to use Tensorflow for the benefits it provides:
-
Excellent documentation, a bunch of helpful tutorials and howtos, which helps developers go deep into Machine Learning solutions;
-
All complex calculations “behind” Python – it uses a unique computational engine and leaves Python free of heavy operations;
-
The building of neural nets and other Machine Learning structures like graphs and chains of single operation blocks;
-
Allows a graphics Processing Unit for much better performance;
-
Supports fast artificial neural network training for efficient deep learning model development.
Tensorflow is widely used in deep learning projects, including applications such as image recognition and recommendation systems.
Tensorflow
If you were developing your Machine Learning application in Tensorflow 1.x and considering refactoring to Keras, try out Tensorflow 2! Keras used to be a powerful and simple wrapper for Google’s Machine Learning framework, now became a part of it. You no longer have to choose between one of them. More benefits of usage Tensorflow 2.0 are shown below:
-
Easy model building with Keras and eager execution;
-
Robust model deployment in production on any platform;
-
Powerful experimentation for research;
-
Simplifying the API by cleaning up deprecated APIs and reducing duplication;
-
Compatibility with Tensorflow 1.x;
While Tensorflow is primarily used with Python, there are Ruby bindings available for some deep learning libraries, such as Torch.rb, which allow Ruby developers to experiment with ML algorithms and integrate neural network training into their applications.
Source: Tensorflow.org
RoR as a Web Application for ML
Ruby on Rails is the perfect choice for web development – it gives developers the possibility to kick off a stable MVP really fast.
However, it does not guarantee the best performance in the quality of complex and heavy computations.
Based on the above, it is a good idea to connect the brilliance of Ruby on Rails framework with Python as a microservice performing Machine Learning computations. This architecture gives us the mix of the best computation efficiency and web application development stability. It minimises the time of building a prototype and provides the best quality of usage. Ruby applications can be extended to support ml applications by integrating with Python microservices, allowing developers to leverage advanced machine learning capabilities within their Ruby-based systems.
What are the main benefits of such a combination?
-
It’s easy and convenient to connect our app with other microservices – the Rails framework provides many reliable ways of communication between different services. It does not break the integrity with the core services. Ruby code can also interface with external code written in languages like Python or C to perform machine learning computations, making it possible to enhance functionality by calling or embedding external libraries.
-
Rails is great for building MVPs – developers can build a web application fast and pitch it to investors.
-
It’s a stable solution with a really good documentation – there are many famous companies which trusted this framework and built efficient software. Its active community support also makes this a smart choice.
-
With the help of gems and Rails packages developers can quickly build more complex parts of an application.
Data Preprocessing and Visualization
Before diving into building machine learning models, it’s essential to prepare your data properly—a process known as data preprocessing. In the context of Ruby on Rails web applications, data preprocessing involves cleaning raw data, transforming it into usable formats, and engineering features that will help your machine learning algorithms perform at their best. Ruby offers several libraries to streamline this process. For example, Numo::NArray provides fast numerical computations, making it easier to handle large datasets and perform operations like normalization or scaling. When working with neural networks, RubyFann can be used to preprocess data specifically for training and evaluating models.
Once your data is ready, data visualization becomes a powerful tool for uncovering patterns, trends, and anomalies. Libraries like Nyaplot and Gruff allow Ruby developers to create interactive and dynamic charts directly within Rails applications. These visualizations help you and your team gain deeper insights into your data, guiding decisions about feature selection and model design. Effective data preprocessing and visualization are crucial steps in the machine learning workflow, ensuring that your models are accurate and your web applications deliver meaningful results to users.
How to Connect Microservices in Python with RoR
So, we chose Ruby on Rails as a web application framework and Python with Tensorflow as a Machine Learning microservice. Great!
The very last element of our technology chain is the efficient connection between these two endpoints. Various communication protocols have been developed to facilitate efficient integration between Ruby and Python services. It is important to choose connection technology carefully.
Let’s compare two most common options:
HTTP Communication
The first option is the HTTP communication – it's definitely the most popular way of connecting the two services.
But the most popular one may not be the best as HTTP protocols are getting older and older. There are still some boilerplates, issues and difficulties along the way. Moreover, it is said that the protocol does not provide the best speed in all cases – since we are looking for the best efficiency in each step of development, it's worth finding something better.
Secondly, this type of communication needs lots of effort in Rails and Python as it results in more time needed to build a stable communication solution.
In the case of Ruby on Rails, it is quite straightforward, but we also need an endpoint in Python. If we chose HTTP, we would have to use an additional tool to build it on the Python side – it breaks the Single Responsibility Principle which we are aware of.
Overall, HTTP is quite complex itself – we need a really simple and efficient way.
RabbitMQ
The second option is a tool called RabbitMQ – it's stable, fast and it is growing in popularity among developers.
The communication model is simpler and faster than the HTTP protocol – outstanding documentation and examples ensure that developers can easily start using this tool. Another important advantage is the presence of truly solid Ruby and Python libraries providing RabbitMQ communication in these languages – making its use really easy and stable.
Using RabbitMQ is a good choice for connecting the Rails web application and Python microservice.
If you want to get started quickly with RabbitMQ take a look at some of their tutorialsto guide you.
RabbitMQ alternatives
Message brokers technology is growing fast. There are several options you can use. They are scalable, secure and efficient. Choose the best for you:
-
Kafka - open-source stream-processing written in Scala and Java. Especially popular in Java-based solutions, enterprise systems and Big Data systems (with cooperation with Spark/Hadoop);
-
Amazon Simple Queue Service - if your application is hosted on Amazon Web Services you can connect it with queueing service with just a few clicks. AWS is a powerful platform capable of building both fast prototypes and scalable solutions. Amazon SQS is just another block to make your application more efficient and reliable;
Best Practices for Using Ruby on Rails in ML
To get the most out of Ruby on Rails in machine learning projects, it’s important to follow a set of best practices that ensure your applications are robust, scalable, and easy to maintain. Start by designing a clear API layer to facilitate seamless integration between your Rails application and machine learning models. This approach allows you to decouple your web development logic from your machine learning features, making updates and maintenance much simpler.
For tasks that require heavy computation, such as training machine learning models or running predictions on large datasets, leverage background jobs in Rails. This keeps your web application responsive and ensures that resource-intensive processes don’t slow down the user experience. Implementing data preprocessing pipelines and maintaining version control for your models are also key steps—these practices help you track changes, reproduce results, and manage the lifecycle of your machine learning algorithms.
Ruby gems like TensorFlow.rb, Rumale, and RubyFann make it possible to add machine learning capabilities directly to your Rails applications. These libraries provide interfaces similar to popular Python libraries, allowing Ruby developers to experiment with a range of machine learning models, from linear regression to neural networks. For more advanced or computationally demanding tasks, consider integrating Python microservices using message brokers like RabbitMQ. This hybrid approach lets you take advantage of Python’s extensive machine learning ecosystem while still benefiting from Ruby on Rails’ strengths in web development.
By following these best practices and exploring the synergy between different programming languages, you can create powerful, efficient, and maintainable machine learning systems that enhance your Rails applications.
Summary
The proposed architecture of web applications using Machine Learning features has both its strong and weak sides.
Web application development is stable, and it’s possible to use Ruby gems to build a web application fast. It ensures great efficiency of Machine Learning computations thanks to the Python and Tensorflow library. Additionally, the connection between both services is fast and safe.
On the other hand, you should consider the downsides as well – it has a slightly more complex architecture model at the cost of creating an almost perfect solution.
In the case of Machine Learning ecosystems, it’s better to mix different technologies and select the best tools to support them rather than rely on standalone choices which are not always as good as they seem.
This architecture enables easy machine learning integration for Ruby on Rails developers, making it straightforward to add AI features to your applications.
For those interested in learning more, there are also machine learning resources and tutorials available in Brazilian Portuguese, making these topics more accessible to a wider audience.
If you need help getting started and want a team with 12+ years experience in developing web applications learn more about our Ruby on Rails teams today.