---
title: A love letter to Objective-C
teaser: Let's take a trip to Objective-C from Ruby.
tags: ruby,ios,oop
author: Ryan Krug
published_on: 2023-06-26
---

The nature of software is to always be evolving. COBOL jokes aside, it's rare to find programming frameworks that reach a level of maturity and support that allow them to just exist without being supplanted by a newer language or a better abstraction. Which naturally is great. Who wants to find themselves writing software with the expectations of today while performing the tasks of manual memory management or manipulating strings that are just raw pointers in a block of memory terminating with a null (`\0`) character?

But sometimes in this constantly evolving space, you find a framework that resonates, and you hold on to it tightly. I wanted to share how this happened for me, first with Ruby (no surprise) but then oddly with Objective-C.

## The Journey

Discovering Ruby was like unwrapping a nested set of presents inside of other presents: 

* "Wait?! Everything's an object?!" -- Running `1.next` in an irb console still makes me chuckle
* "Who's _why?"<sup><a href="#fn1" id="fnr1" title="see footnote" class="footnote">1</a></sup> -- I still prefer the [Poignant Guide](http://poignant.guide/book/chapter-1.html) to the [Pickaxe](https://pragprog.com/titles/ruby5/programming-ruby-3-2-5th-edition/)
* "No way. You can't build a blog in 15 minutes?"<sup><a href="#fn2" id="fnr2" title="see footnote" class="footnote">2</a></sup> -- Rails exposed me to [convention over configuration](https://rubyonrails.org/doctrine#convention-over-configuration), the [active record pattern](https://en.wikipedia.org/wiki/Active_record_pattern), the [model-view-controller pattern](https://en.wikipedia.org/wiki/Model–view–controller), and [test driven development](https://thoughtbot.com/upcase/testing) all at once

But then the iPhone shipped and __everyone__ wanted to write an "app" for it, myself included. There was a catch though, you had to write it in Objective-C. Which, if you think is an obscure language today, check the [TIOBE index on Objective-C](https://www.tiobe.com/tiobe-index/objective-c/) for 2007 at the launch of the iPhone. 

Also, discovering Objective-C had a little rougher beginning:

* "Wait?! I'm typing square brackets around everything? What is this?!" -- Objective-C __is__ C, extended in new ways via square brackets
* "Why does every class start with the letters 'NS'?" -- Namespaces are not a thing, so avoid collisions by prefixing all your class names
* "Where is my garbage collector?" -- You are still on C, so keep track of those `retain`/`release` cycles or segfault and cry

But after you wipe the tears away and the dust settles, you can begin to understand that at it's core, Ruby and Objective-C actually share a lot in common: 

* Ruby has `Object` that all classes extend from, Objective-C similarly has `NSObject`
* Ruby has `Proc` for passing methods around, Objective-C has [blocks](https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/ProgrammingWithObjectiveC/WorkingwithBlocks/WorkingwithBlocks.html)
* Ruby has open classes for monkey patching, Objective-C has [Category and Extensions](https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/ObjectiveC/Chapters/ocCategories.html) to do the same
* Ruby has `Object#respond_to_missing?` and `Object#method_missing` for metaprogramming, Objective-C has a very analogous `NSObject#respondsToSelector` and `NSObject#performSelector`
* Ruby has `Object#send` for sending raw dynamic messages to objects, Objective-C runs every message through `objc_msgSend`
* Ruby has runtime/reflection methods for querying objects and changing instance variables like `Object#methods` and `Object#instance_varaible_set`, Objective-C allows you to interact [directly with the runtime](https://developer.apple.com/documentation/objectivec/objective-c_runtime?language=objc) as well

Architecturally, they actually share enough in common that there have been [quite](https://en.wikipedia.org/wiki/RubyMotion) a [few](https://en.wikipedia.org/wiki/MacRuby) [attempts](https://en.wikipedia.org/wiki/RubyCocoa) at bridging the two together over the years. The [earliest of which](https://gnustep.github.io/experience/RIGS.html) I discovered dates back to 2001 and was built as part of the [GNUstep project](https://en.wikipedia.org/wiki/GNUstep) -- an open source implementation of Apple's Objective-C and Cocoa frameworks.

Much has changed since then in both the Ruby and Objective-C worlds, but I was surprised that after some tweaks to call modern versions of its Ruby C extension code and Objective-C runtime, and then accomodate for 64-bit architectures, the project's code [runs on modern versions of macOS and Ruby](https://github.com/keegnotrub/obj-ruby). It's really a testament to the Objective-C runtime (and Ruby C extension API) that while much of the original code was written in 2001, an incredible amount of it runs unchanged over 20 years later on modern versions of macOS and Ruby.

## I Don't Need Your Rockin' Chair

Now, it's not lost on me that Objective-C currently shares a spot on the [TIOBE index](https://www.tiobe.com/tiobe-index/) right alongside COBOL in 2023 (position 25 and 26, respectively). Apple's attention turned to Swift in 2014, and they haven't looked back. However, even today in iOS 16, Apple ships [over 66% of their native binaries](https://blog.timac.org/2022/1005-state-of-swift-and-swiftui-ios16/) still using Objective-C. The language has respectably reached that level of maturity and support that allows it to just continues on, quietly running in the background.

<ol class="footnotes">
  <li id="fn1">While active, <a href="https://en.wikipedia.org/wiki/Why_the_lucky_stiff">why the lucky stiff</a> was a kind of Banksy character in the Ruby world, delivering all kinds of fun one-off Ruby work under the pseudonym "_why".<a href="#fnr1" title="return to article" class="reversefootnote">&#8617;&#xFE0E;</a></li>
  <li id="fn2">"How to build a blog engine in 15 minutes with Ruby on Rails" was the <a href="https://twitter.com/dhh/status/492706473936314369">official marketing video</a> for the framework in 2005.<a href="#fnr2" title="return to article" class="reversefootnote">&#8617;&#xFE0E;</a></li>
</ol>
