Did you know that constant lookups are slow in Ruby?

Laurent and I were having a conversation one day about things in Ruby (and RubyMotion) that were slower than one might expect. The big one that popped out at me was that Constant lookups are slow.

That was crazy to me, as I assume that FOO = 'Bar' is the fasted way. It turns out that Ruby’s rules for constant lookup are so complicated that only the MRI implements all of them. Because of this, the process is slow.

The fastest thing you can use to store ‘Bar’ is to use @foo = 'Bar'.

It’s all relative of course and “slow” in this case is still very fast compared to most of our application code. However, if you are in a tight loop and you need the highest performance possible it’s good to know about. Also, Laurent said he caches the constants in the RM runtime to help with this problem.

Method invocation (calling a method) is also one of the slower low level things to do. So for maximum performance, you can inline them into one method.


If you missed the last issue, you can find it here: issue #37.

Happy coding, Todd Werth (@twerth)

Articles, News, New Gems, and Blog Posts

July 28th, 2014 | screencast | by Jack Watson-Hamblin
MotionInMotion – Episode 35 – Tab Bar Controllers And How To Customise Them

July 24th, 2014 | training | by Colin T.A. Gray

July 22th, 2014 | book | by Clay Allsopp
Updated to iOS7 and RM 2.0 – RubyMotion iOS Development with Ruby

July 16th, 2014 | video | by Gant Laborde
Motion Meetup – July 2014 with Colin T.A. Gray

Hidden Gem by Gant Laborde

My text fields aren’t selectable!? Tapping them doesn’t make them becomeFirstResponder!? What am I doing wrong!?

When adding a group of interactive fields to a view, people like to add them grouped, by superview. Often times, in RMQ you’ll see something similar to this:

rmq.append(UIView, :form).tap do |form|
  form.append(UITextField, :some_input1).get.becomeFirstResponder
  form.append(UITextField, :some_input2)
  form.append(UITextField, :some_input3)
  form.append(UITextField, :some_input4)

The same can be done by a bunch of addSubview(input) calls, for non-rmq.

QUESTION: Why would it be that you can’t select a text field with a tap?

  • Enable User Interaction? Nope, each subview has their own event queue.
  • Animating? Nope. Though user interaction is disabled on animation (unless UIViewAnimationOptionAllowUserInteraction is set), and that code isn’t animating.


You can lay everything out, but it’s very important that you make sure the superview (:form in this case) is the right size. If not, then your text fields will be visible but not selectable. In these situations, it’s best to set a holder UIView with clipsToBounds = true to avoid a form growing beyond it’s size, or simply being misaligned down the road.


My company InfiniteRed sponsors this newsletter by allowing me the time to work on it every week and hosting everything.

Contact us if you ever need help working on a project, mentoring, or other development services . We specialize in RubyMotion and have an awesome team.


One more thing

This is useful for Objective-C documentation and sample code. It’s not 100% perfect, but I use this a lot to get started.

Objective-C to RubyMotion Converter

If you have any tips, blog posts, or want to sponsor this newsletter, please send emails to todd@infinitered.com