Skip to content

Instantly share code, notes, and snippets.

@chrisgaunt
Forked from Bodacious/Explanation.md
Created May 19, 2013 06:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrisgaunt/5606910 to your computer and use it in GitHub Desktop.
Save chrisgaunt/5606910 to your computer and use it in GitHub Desktop.

RubyMotion comes with some syntax idioms that are not strictly idiomatic Ruby.

For example, in an iOS app we'd often see these two methods defined in a subclass of UITableViewController

    -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
      // callback code for selecting table rows goes here...
    }

    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
      // define the cell that should be returned at this IndexPath
    }

Strictly speaking, these method names are tableView:didSelectRowAtIndexPath and tableView:cellForRowAtIndexPath - this comes from Objective-C's awkward syntax of defining parameters within method names.

To try and replicate this in Ruby, Ruby Motion uses a syntax that looks similar although semantically would carry a different meaning in idiomatic Ruby.

    def tableView(tableView, didSelectRowAtIndexPath: indexPath)
      # callback code for selecting table rows goes here...
    end

    def tableView(tableView, cellForRowAtIndexPath: indexPath)
      # define the cell that should be returned at this IndexPath
    end

Note that in Ruby, this would typically mean that a method named tableView had been defined, with the first parameter being named tableView and the second parameter being passed as a hash of options*. Also note that both of the methods defined here have the same name which, in a standard Ruby compiler, would result in the first method being overwritten by the second.

In RubyMotion, both methods are defined and compiled to their equivalent Objective-C methods in the iOS SDK.

*This is a syntax added to RubyMotion and would raise a syntax error in a normal Ruby interpreter. This was added to conform to the way Objective-C methods are called, Objective C methods have named parameters.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment