Blog

Comparing Ruby and Objective-C

The rise of the polyglot programmer is now. In the past, programmers would dedicate their careers to a language and become experts in that domain. With the ubiquitous availability of documentation, books, and resources to learn languages, developers these days are no longer bound to this and are freely available to choose the language for the job at hand. Concurrency? Scala, Clojure, Erlang, Go, and more are all available. General purpose web applications? Pick your poison of scripting language. Mobile apps? Well, there is PhoneGap and Titanium, but iOS is really the domain of Objective-C. All of these are well supported by growing communities that generate the resources required to quickly learn these languages.

Today we'll focus on a couple parts of two languages: Ruby and Objective-C.

History

Both Ruby and Objective-C have their roots in other languages. These languages both draw influences from Smalltalk, a purely object oriented, reflective programming language. This has given power to both of the languages with a lot of metaprogramming capability. Objective-C adds this on top of C, while Ruby is derived with inspiration from Perl.

Typing

Ruby is a duck typed language. Duck typing determines the type of an object with it's current set of methods and attributes. Objective-C on the other hand does not have this feature, opting for static typing that is determined in advance. Both languages are dynamically typed, in that type checks happen at runtime.

Reflection

Reflection is a feature available in both Objective-C and Ruby. This means that both languages can observe and even change their own structures at runtime.

For Ruby, this form of metaprogramming looks like this:

class MyClass
  def greeting
    "Hello, World!"
  end
end

myclass.send(:greeting) => "Hello, World!"

In Objective-C, it looks something more like this:

MyClass *greeter = [[MyClass alloc] init];
SEL selector = NSSelectorFromString(@"greeting");
[greeter performSelector:selector withObject:nil]; # => "Hello, World!"

Though Objective-C may be more verbose, it is fully capable as a powerful language for metaprogramming. However, the code above brings us to another point: method calls.

Method Calls

Method calls in Ruby are very straight-forward: you perform a method on an object, class, or module and it calls it. If the method is not found, it falls up a chain of method_missing methods that can be redefined. If nothing responds, then an error is thrown.

Objective-C has taken on the Smalltalk idea of message passing. Instead of calling methods, classes and objects are sent messages, which then invoke methods on the object. The difference is more philosophical than practical, and the book Object Thinking has an excellent treatise on the subject.

Though Ruby and Objective-C are different in their uses and ideals, they share many of the same paradigms. With a little bit of knowledge about memory management, a high level programmer can expect to dive into it head first. Pick up a copy of XCode, install the SDKs, and have some fun!