A History of Ruby in iOS Development
A History of Ruby in iOS Development
It's a common question: why do iOS developers use Ruby so often? This article delves into the history of Ruby's involvement in iOS development to answer that question.
Join the DZone community and get the full member experience.Join For Free
The 24th of February marks the 25th birthday of the Ruby programming language, named by Yukihiro "Matz" Matsumoto in Japan. Since released in 1993, it has become the most dynamic, easy-to-learn, and fashionable programming language. In the world of iOS development, Ruby has been used almost everywhere, from package management to automating releases or managing Xcode projects using scripts. Why is that? What is Ruby doing here in the world of iOS development, even there is nothing in common between iOS and Ruby? In this post, we will try to find out some of the historical reasons for Ruby's existence in iOS development and deployment.
What Made Ruby Popular
Now, we will cover what sort of Ruby-based tools have been used in iOS development.
Ruby Tools in iOS Development
There are various Ruby-based tools being used actively in iOS app development for various reasons. Recently, someone asked a genuine question on Twitter: "Why is it that iOS developers often use Ruby?"
This is a valid question as there is hardly any iOS project which doesn't have Ruby inside it, apart from those developed at Apple. Now we will list afew tools which became very popular among iOS developers and became a core part of iOS development:
These are just a few Ruby-based tools, but there might be many more used in iOS app development under the hood. Now, we will look into a little bit of background of these tools.
The nomad-cli is not actively used in recent days as it been superseded by Fastlane tools, however, Fastlane tools were inspired by nomad-cli, which is written using Ruby. This was one of the early players when Ruby-based tools started to appear in iOS development. This project built a solid base for the command line tools that can be written on top of nomad-cli. This is pure command line interface used for distributing iOS apps to the App Store. You can read more about nomad-cli here.
The Rise of nomad-cli
Before nomad-cli, apps normally used to be archived using local Xcode, or there needed to be complex scripting using xcodebuild tools. The scripts produced by xcodebuild are usually very lengthy and hard to maintain. It accepts a lot of parameters and each parameter needs to be configured in the script, which is tedious. The author of nomad-cli, Matt, was skilled in Ruby and built this tool to solve iOS binary distribution problems using the command line.
Why Is nomad-cli Written in Ruby?
I'm not sure why the author of nomad-cli started using Ruby for building the wrapper on Apple’s native technologies. Looking at mattt’s GitHub repos, he seems well-versed in Ruby, so that might be the reason he chose Ruby for nomad-cli.
CocoaPods is the project where Ruby sets foot inside iOS app development. CocoaPods is a dependency management system for Xcode projects. It's been used to create and distribute Swift and Objective-C packages and is reused in other projects. CocoaPods has a centralized repository of all packages that can be used in Xcode projects. CocoaPods is not a single project but a collection of projects written using Ruby. The projects include Xcodeproj, CLAide, Molinillo, CocoaPods Core, and Master Repo. You can find the details of the projects here.
The Rise of CocoaPods
Before CocoaPods, managing dependencies inside Xcode projects was a real pain. iOS engineers had to link the frameworks manually within Xcode build phases. CocoaPods solved this problem by automating all these tasks internally.
Why Is CocoaPods Written in Ruby?
The main author of CocoaPods, Eloy Duran, answered that question in this tweet. According to him, Objective-C and Ruby are both influenced by Smalltalk. It’s also why he worked on various Ruby implementations on top of the Objective-C runtime. The reasons specified for selection of Ruby include:
- Easy-to-share code with Bundler/RubyGems.
- In the early stages, leverage MacRuby (Ruby on Objective-C runtime) so we can use native PropertyList (de)serialization APIs to read/generate Xcode projects and defer pure Ruby solutions to later.
- Dependency management is, in general, hard to get right for everybody’s situations - definitely when just starting out, and even more so in complicated native environments like Xcode projects. Ruby was a good choice to let users monkey-patch whatever they needed for their projects.
- Ruby is an excellent language for CLI tools that do a lot of string manipulation.
- They made an effort to make sure CocoaPods would work with the Ruby that shipped with OS X, even without the Xcode CLI tools installed.
CocoaPods uses the Xcodeproj gem to modify Xcode projects. After Xcodeproj, there are loads of Ruby libraries built which deal with Xcode projects using the command line or scripts.
Fastlane is widely used these days to automate the building, testing, and distribution of iOS apps. Fastlane is used to script infrastructure as code that can be run on any Continuous Integration server. It has a set of tools to automate various iOS development tasks. You can read more about Fastlane here. All the Fastlane tools are built using Ruby. Recently, the Fastlane team at Google has been building Fastlane-CI using Ruby.
The Rise of Fastlane
As mentioned earlier, Fastlane tools are heavily inspired by nomad-cli. Fastlane has automated most of the iOS development and deployment activities, like building, testing, code signing, screenshots, test flight, and many more. Fastlane has built extensive and scalable wrappers on Apple’s native technologies and is usually very quick to adopt any changes made by Apple in the native tools.
Why Is Fastlane Written in Ruby?
As per the Fastlane tools author, Felix, he also ended up using Ruby to be able to directly access and use the classes they already wrote to support Xcode projects in the previous tools, e.g. CocoaPods and nomad-cli. Felix thinks that there are a few reasons:
- mattt was an early player in the iOS tooling scene, and he built nomad-cli in Ruby (a big inspiration for him).
- CocoaPods was written in Ruby, so when starting Fastlane, he wanted to integrate with both toolsets from day 1.
The main author of Danger, Orta, also contributed to many other open-source tools, including CocoaPods and Fastlane, and also built libraries. Most of the interesting Ruby-based libraries were developed at a company called Artsy. You can follow Artsy on GitHub or follow their blogs for interesting projects.
xcpretty is a formatter for
xcodebuild which can be used to generate nice-looking reports in different formats. Currently, it supports JUnit, HTML, and JSON formats. It also helps to make
xcodebuild logs more readable. We are not going into the details of why the author chose to build this tool in Ruby. We can safely assume that major tools were built in Ruby, so they probably wanted to align all the tooling in Ruby.
xcov is a tool to display code coverage reports in a user-friendly format. It generates reports in HTML, JSON, and Markdown formats. xcov is also written in Ruby. You can read more about xcov here. We are not going into the details of why the author chose to build this tool in Ruby, since we can safely assume that major tools were built in Ruby, so they probably wanted to align all the tooling in Ruby.
Can iOS Development Be Ruby-less?
The answer is YES and NO. If iOS developers spend the time to understand and use the native iOS tooling from Apple in their own workflow, then YES. However, I don’t think it’s going to happen very soon. Apple’s native tools are complex and hard to learn. Apple is continuously trying to make developers happy by introducing some GUI-based features into Xcode on top of the native technologies, but so far, no success.
In order to have a Ruby-less iOS project, someone would need to write an alternative tool for CocoaPods and Fastlane, which is hard.
There are projects like Carthage and Swift Project manager, which can be used instead of CoacoPods, but both have some limitations to being fully adopted in the iOS workflow. Carthage has too many manual steps, and Swift Package manager is still not ready to work iOS projects. One of the possible alternatives for Fastlane tools would be writing custom scripts on top of Apple’s native command line tools. This requires a deep understanding of Apple’s technologies and amazing scripting skills. It would be rare for iOS developers to have those skills by default. Fastlane tools have already done too much of the work to automate most iOS development workflows, so they can be used as is without pain.
You can remove Ruby completely from iOS projects, but you need to learn more about Apple’s native technologies, work hard, and reinvent the wheel. It’s worth doing if you have the time and money to do it.
Ruby has solved complex Apple developer tools problems by creating tools like CocoaPods, Fastlane, and many more. Most iOS developers already accepted Ruby in their own workflows, so Ruby isn’t going away from iOS app development anytime soon. It would be worth it for every iOS developer to find some to learn Ruby. It would be interesting to see how Apple developer tools can deal with Ruby’s dominance in the iOS world. What are your experiences using Ruby in iOS projects? Share in the comments below.
Published at DZone with permission of Shashikant Jagtap , DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.