tek, thinks & la strada ☯ॐ☢ csmr@kapsi

Cell selector prog: Rust vs Ruby part I

I was recently asked to complete a test technical task as part of a recruitment process. After thinking of how to make this fun, I decided: Let's compare Rust and Ruby programming languages and their tooling. A segway into Rust development? I certainly hope so!

Why not JavaScript or TypeScript? Ruby as a language is far more advanced. Further, Ruby tooling like RubyGems and Bundler pioneered package management tools for programming languages, setting the standard for other languages.

The Core Problem

The problem at hand: selection of the best cell station for phone at some location. A cell is the area supported by a link station, and a cell phone checks regularly which one it should connect to for maximum bandwidth, which in turn is determined by momentary maximum transmission power. Transmission power of radio waves is inversely proportional to square of the distance (Source). Note that I will further elaborate the details this problem in part II.

So, a lot is known, the spec seems to be enough to implement a solution. On with the show!

Create a git repository for the project

Why git? Because its better than mercurial. Git helps you manage your changes, and is factually the current industry standard.

Bash session with git init command

Let's add a directory for both languages, and implement a separate solution in each. The directory tree now looks like this: Bash session with git init command You can ignore the blog -directory, its what I'm using to write this blog.

Starting a project in ruby and rust

One could argue that the ultimate problem in practical software is managing libraries that your program depends on. Unless ones app is completely without side-effects of IO, one will likely have to consider the problems related to consuming external libraries.

Even worse, the dependencies of the libraries of your dependencies would take a lot of time. Through automation, a developer can emerge from dependency management churn, to focus the core problems of the Real World.

Happily, both Ruby and Rust communities are woke to this predicament, and there are sophisticated solutions for managing dependencies. As a kind of side effect, the dependency management tools like Bundler and Cargo also offer features to manage other aspects of projects files, such as testing.

Ruby, Gems and Bundler

Installation and the dependency management of ruby programs are managed with RubyGems in ruby. Further, Bundler was created to ease the management of gems.

New ruby project (a gem) is created with bundler-magic: bundler command

Bundler will ask several questions and initialize the project accordingly. I chose to include an MIT license, minitest framework, and exclude CI, changelog and linter setup. Bundlers main configuration is in Gemfile, and the resulting directory tree looks like this: Ruby gem project directory tree created with bundler

That didn't hurt one bit! Instead it made me look into which test framework one should use (probably RSpec, but chose minitest for this mini-project). The tautonomy between the main directory name and the package is obvious so let us change that to avoid (reduce) confusion - rename the main dir celca. mv command for renaming directory

That is clearly a step forward so its best to save these changes into the repo: git commit command output

New rust project with Cargo

As an historical context to Cargo, bunch of ruby devs wrote Bundler, and later one of them, Yehuda Katz wrote the rust dependency management tool: Cargo, and then went on to work on NPM's problems in Yarn. Cargo helps with many aspects of packaging, dependencies, installation, testing and execution of rust programs. So, several other features aside from dependencies.

Let us make a new project with cargo, after installing rust for Debian 10 with this script. git commit command output

Cargo new created just two files, Cargo.toml and src/main.rs. Since the ruby version of the package includes the test stub thanks to bundler, let us bring the rust version up to date as well. Default rust main.rs with test stub added Rust programs include macros for unit tests in the same file as the logic. Test stub complete. Happily, there's now two projects to work on!

This ends the part I of this test project to compare ruby and rust. Part II announced soon.

Copyright Casimir Pohjanraito 2021

Copyright C. P. - Last Updated - All Rights Reserved - Ask permission to republish.