Ruby and Me

Around February, after I posted rice v3.5.0, I promised to make a blog to explain how I use Ruby as a shell. Initially I want to explain it on an article, then it expanded into a website! Then a mountain of college work and internship job drop into my schedule... After all of that, I finally have time and energy to blogging again. So sorry and thank you everyone for waiting to follow my blog, I will try blogging more frequently!

Back to Ruby. As mentioned, the website that I created is Ruby on Shell and you can read it right now. This blog post will focus on how I discovered the power of Ruby and the process by which I created Ruby on Shell.

How I get to know Ruby

I was a naive programmer back then, thinking that I should just master one programming language and use it for everything (and that language was Python, a very normie choice...). I used Xonsh (in short, it is a fusion between Python and Bash) as my main shell for a while, its interactive experience is quite good but scripting on Xonsh is a bit annoying. Because of Python's off-side rule and list comprehension, it's hard for me to scripting as short as I'd like.

I want a full-fledged programming language like Python, Perl or Lua but with good scripting features like Nushell or Elvish.

Then one day I saw this line on an article by Xah Lee about list comprehension:

(0..9).select{|n| n.even?}.map{|n| 2*n}

Note that this is not list comprehension, because it does not use a special syntax. But it captures the map(f, filter(list,predicate)) in ruby style.

I've seen Xah Lee mention Ruby a few times, but that's the first time I've read a Ruby code and understand what it does. Its syntax is very compact, I feel that I can script on this language easily. And I was correct, I've dived into why Ruby is a perfect choice for shell scripting in Ruby on Shell so I won't repeat it again.

Here are my few thoughts on Ruby before moving on to the next section:

  • I have been peeking at Lua for a while now, I even found a shell similar to Xonsh but using Lua as the core (Hilbish). But I don't really like Lua because even though it's fast, light, and simple... Its Syntax is still quite verbose and isn't easy to chain functions from left to right like Ruby or other shells.
  • Currently, there is no language to easily run both Ruby and shell commands, so here is how I use Ruby in the terminal:
    • Use a POSIX shell as the main shell, and create a keybind in the shell to switch to Ruby for scripting.
    • In Ruby there is a syntax to call shell commands easily (wrap the command with backticks) so just enjoy shell scripting.

Ruby on Shell - How It's Made

Not difficult but very time consuming:

  1. Make a list of standard CLI tools: I found it from the POSIX documentation, the list of tools in Toybox, and a few other sources. Then put it all together and go through the list to see what should and shouldn't be on the list.
  2. Find the alternative in Ruby: Again go through everything on the list, find a similar function or library in Ruby and document it. Most of the time, I just need to add a link to an API, but some have to write a more detail instructions.
  3. Build a proper website: Although this is the hardest part of the project, it only took me less than a week to finish the frontend... And about two weeks to writing the introduction.

It was hard work, but it was worth it in the end. The process helped me increase my understanding of Ruby, plus now I have a pretty useful cheat-sheet.

The Future

I'm currently working on a project called Shell on Ruby to make scripting and using shell commands on Ruby more convenient.

I'm also occasionally peeking Crystal, its syntax is ~90% similar to Ruby but faster and has some better design than Ruby, but scripting on it isn't as convenient.

And that's all, I will continue my adventure on making a perfect scripting environment. See you in the next blogs.