Learning Ruby for the first time because of fastlane

Last week I programmed in Ruby for the first time to implement new iOS automation tasks in fastlane. I’m originally a web developer who used PHP, so I’ve heard of Ruby many times as a competing backend scripting language. For years I had thought of Ruby as an old language that was on its way out, but after working with it for a few days I couldn’t have been more wrong.

fastlane — the tool to release your iOS and Android app

Ruby Syntax 🤓

I’m not familiar with fastlane, so the beginning of my Ruby learning was figuring out what was actually Ruby and what was specific to fastlane in the Fastfile. As I dug around the Ruby documentation to achieve what I wanted to do, I noted a few things that stood out to me about the language.

  • Functions can be called with or without parentheses

  • Class instance variables need attribute accessors

  • Class static variables need setter/getter functions

  • Variable naming conventions: $global, @@classstatic, @classinstance

  • Snake case….! Only part of Ruby that felt old 😜

  • Exclamation mark in a method’s name signifies it modifies the variable… cool!

  • Last executed line of a method is the return value 🤔 (tricky with try/catch blocks called begin/rescue in Ruby)

Ruby — Example class declaration / instance creation

Once I got the hang out the basic structure of classes, functions, naming, and declarations, I was quick to get my code working. I found the majority of the needed documentation online from ruby-doc.org and the rest from a variety of different sources through Googling the runtime exception errors.

It felt refreshing to be back in a scripting language which didn’t have any compile time, unlike Swift. Iteration was quick and the error messages from the language were helpful to lead me to a solution. My error messages were either from fastlane or from the Ruby language. Every time a fatal exception is thrown, fastlane will search GitHub issues for the exception message so it was easy to tell which domain the error belonged to.

Ruby — exceptions printed in console

Functional Programming in Ruby🔒

I was happy to find some functional programming functions implemented in Ruby for the Array class. Ever since I attended Swift Summit in late October, I’ve wanted all my new programming to be as functional as possible. Fewer side effects make everything better, especially tests. While using map in Ruby doesn’t guarantee anything like that, it’s still cleaner syntax than the traditional sum of an array.

I found Ruby’s block syntax for map and each to be straightforward. When I created my first map block in Ruby I ended the last line with a return statement only to get a runtime error. After debugging, I learned the last line of the block is the return value…interesting. Personally, I don’t mind a return statement in the last line and I think the code doesn’t look any cleaner without the return keyword. However, that is how its implemented in Ruby-land, so I followed suit.

Ruby map

fastlane + Ruby = 🚀

When I think of traditional automation of computing tasks, I think of bash scripting or Python. I didn’t realize that Ruby would be so fit for the automation task… or is it? There are other languages that could fill the same hole but Ruby seemed to be a great candidate. I felt powerful in the Fastfile, which I can attribute to both fastlane and Ruby together.

Fastfile — test lane: fastlane test

Learning Ruby

Ruby language — Official logo

My opinion of Ruby is only in the fastlane environment, so I need to try using Ruby without using fastlane to get an accurate assessment of the language. However, using Ruby in the fastlane environment was a positive experience. It was surprisingly natural to learn Ruby and fastlane together. It’s rewarding to learn a new language because it brings in a new perspective to programming which allows me to compare and contrast languages and think of problems in different ways. Every language has its own features and drawbacks, so knowing more languages reveals a large library of language features and implementations.

I still have a long way to go, but I’m already looking forward to learning more Ruby when the opportunity presents itself! 🙌