Fork me on GitHub

Started: 17 May 2012
github

SugarCube was a pet project at fusionbox that I wrote to keep UIConstantNames a little more palatable to me by extending the Symbol class to support all sorts of conversions to UIKit contants.

At least, that's how it STARTED. These days, SugarCube is a set of helpers and tools that make the REPL more powerful, extend base types to make code terser, and in general just make life a little more rubyesque, while we live in this odd little "Is it iOS? Is it Ruby? Yes to all!"-world.

Because the project gained so much traction, it was moved to the community repo at https://github.com/rubymotion, and even though I remain the lead developer of the project, it is picking up pull requests and bug fixes from the community!

New to sugarcube?

The rough goal of SugarCube is to make iOS development with rubymotion "sweet".

In that spirit, the types of things it provides are (see the README for details and more examples):

  • Helpers. UIKit is verbose, and it just doesn't feel like ruby should be that way. These helpers range from straight-up aliases (UIView#addSubview => UIView#<<) to some mighty powerful abstractions (UIView#first_responder, which finds the first responder in your view hierarchy). Factory methods are also added, so instead of UIButton.buttonWithType(UIButtonTypeCustom), it's more pleasant to type UIButton.custom. or UITableView.grouped, UITableView.plain. You get the idea.

  • Type transforms. The idea is this: instead of [UIClass method:target], target.uiclass. If there is a second argument, try and make that optional.

For instance:

  "logo".uiimage  # => UIImage.imageNamed("logo")
  "logo".uiimage.uicolor  # => UIColor.colorWithPatternImage(UIImage.imageNamed("logo"))
  "logo".uicolor  # same thing ^^^^^
  "https://github.com".nsurl => NSURL.urlWithString("https://github.com")
  :blue.uicolor  # => UIColor.blueColor
  :blue.uicolor(0.7)  # => UIColor.blueColor.colorWithAlphaComponent(0.7)
  "Inconsolata".uifont(20)  # => UIFont.fontWithName("Inconsolata", size:20)
  # optional second argument
  "Inconsolata".uifont  # => UIFont.fontWithName("Inconsolata", size:UIFont.systemFontSize)
  "Welcome"._  # => NSBundle.mainBundle.localizedStringForKey("Welcome", value:nil, table:nil)
  • Constants. Don't use 'em if you don't like 'em. I like 'em. e.g. UITableViewCellAccessoryDisclosureIndicator => :disclosure.uitablecellaccessory. There is a naming scheme in SugarCube. if you study symbol.rb, and use these often, you'll get the hang of it.

  • Tools. That's a fancy name for "dumping ground for stuff I want to be made shorter and/or easier". Things like:

  if "file.plist".file_exists?
    objects = NSArray.arrayWithContentsOfFile("file.plist".document_path)
  end

  case index_path
  when IndexPath[0, 1..5]
    # ...
  end

  "http://google.com/?q=#{search.escape_url}".nsurl  # nsurl is a type transform, but escape_url is an NString addition
  UIAlertView.alert("Hi!")  # this had a bug that was fixed in this release, btw.
  • REPL wonderfulness. Viewing the subview hierarchy: is that not the best thing ever? If you are not using this, you are missing out. See tree.

You can also make view adjustments, and if Thom Parkin ever submits his cool view centering thing, we'll have even more tricks! Come on, Thom! :-)

  • Some CoreGraphics stuff. But having looked at Clay Allsops's geomotion project, I feel like there is a duplication of code thing going on here. Not that that's a bad thing in and of itself, but I think geomotion is better, and so the core graphics additions might get dropped in favor of encouraging that gem.

Opyh also has some cool stuff going on in motion-ui-geometry. I think we should combine our efforts. For the win, of course.

Note Clay and I did merge SugarCube's helpers into geomotion, so don't try looking for them in SugarCube :-)

  • I try and keep the README as up-to-date as possible, so go there for examples and more info.

Here's the original announcement at fusionbox:

http://fusionbox.org/projects/rubymotion-sugarcube

Here are the different announcements I've made about SugarCube: