What I always enjoyed while learning a new programming language was that even if in the end I cannot stick with it, I at least can enrich my daily working life with something that this another language is good for.
That happened to me 10 years ago when I discovered Ruby and especially Ruby On Rails. Back that time, I was a PHP developer and was amazed by the “magic” that came from Rails. Before finally switch to Ruby I constantly tried to implement my PHP code in Rails way. Even though it looked sometimes weird, it worked pretty well and did the job.
The same thing happened to me this year when I step on my path of learning AI development and did some progress in designing neural networks using Python. And of course, I discovered Jupyter notebooks – one of the most popular tools for Data Scientists.
The Jupyter Notebook is an open-source web application that allows you to create and share documents that contain live code, equations, visualizations and narrative text. Uses include: data cleaning and transformation, numerical simulation, statistical modelling, data visualization, machine learning, and much more.
It was just a matter of time until I thought about using it with Ruby and you know what? I did not regret it. Let me tell you a couple of features that will probably change some of your daily routines as a ruby programmer in a better way.
Jupyter is a web app written in Python, so the very first step we need to do is to get your things dirty with Conda and pip. I do not even mention that you must have a fresh Python version on your machine. Next thing would be to install actual Jupyter. Finally, to be ready for some action, you will need to install iRuby which allows you to use Ruby within Jupyter. That’s it. Now you can just start the session with this command
jubpyter notebook . .
That’s it. Now you can create notebooks and execute Ruby code right from them. And here comes the main question — what are the benefits?
The basics of Jupyter
Let’s start with some basics first before diving into the benefits that Jupyter may bring to you. Each notebook may have many cells. That can be code or markdown. You can execute the code directly in the cells and the result will be stored within the notebook file. This is super handy, as you can host this notebook on Github (it supports them) or just send it to somebody and once it’s opened even without execution one may see the previous results.
html or even converted into presentations and standalone web applications.
Alright – alright. We got it, and still – why do we need this? From the description, we know that Jupyter is good and mainly used in Data Science world. In other words, it is fit for retrieving, preparing and processing the data, designing and training the models. But that does not stop here. One may store documentation for a library or write a book that contains code blocks that one may execute right away by hitting ⏯ button. That are just a few examples.
We all like Gists, do we? They are quite handy and allow us to store some piece of code or complete scripts and share it with our colleagues or the whole world. However, the biggest issue I usually face with Gists is that you need to do certain things before you can even execute that code. Let’s start from the output — say we found a Gist with some snippet of how to call an endpoint and process its result. What is the response of that code? Do we get Array or instance of some class or simply
nil ? We will be lucky if the author added some comment describing this part, but quite often this is not the case. With Jupyter, the results of the previous execution are stored within the notebook itself, so we can immediately see what this code does and what it returns. In the end, if you still want to keep on using Gists you can convert a notebook back to a simple gist using the plugin.
Some of the other handy examples can be found here.
My main use case of using Jupyter is playing around with the code or making some documentation. Quite often I have to try out different external endpoints. However, this is not just about doing a REST call, but also playing around with the response. So traditionally without Jupyter, I would use curl, or (most likely) IRB. The problems that happen regularly are that once you are done with the investigation, you have a history full of different steps you did – some of them were successful, some of them consist of misprints and now you have to recall the right order so that you can copy the final script to someplace in order to find it later like gist, for example. With Jupyter you do this automatically. Each time you execute code you already make it clean, you make it work. The only thing you will have to do in the end is to clean up some no used cells and maybe add comments.
Things maybe not that good
Jupyter was built with Data Science and Machine Learning in mind. Even though it was extended and adopted by other languages, it is still a web app written in Python with Data Science in mind. Jupyter works fine with Ruby unless you need to do something especially complicated, something that is not usual, something that does not fit the basic usage like including a gem and executing some code. For example – including the whole Rails project so that you can do some ActiveRecord queries or accessing some other classes can be a tricky task. Or what about executing the whole RSpec test suite from inside Jupyter e.g. programmatically? The problem is not that this is not possible, but because nobody did that before, you will not be even able to google for it. In this case, you will have to dig into the source code or documentation to find a solution. To say frankly, this is not that bad in the end, but this will take quite some time to get used to.
On top of that, think about how many Rubyists have Python installed along with their favourite language. That does not really matter unless they decide to modify and run the code from your notebook for which they will have to install Jupyter, Python and all the dependencies.
Do not give up
Jupyter is a great tool that works with many programming languages. It fits many needs and use cases and works pretty nicely with Ruby and may enhance your daily working life. However, that does not come for free. Ruby usage with Jupyter is not a popular thing, so in some cases, especially when you try to do something completely special you will be on your own with that. Even a search engine will not help you as there will be simply nobody out there in the world who did that before you.
On the other hand, if you find your own path and adopt the Jupyter for your needs it will definitely simplify a bit of your daily programming life just like it happened with me.