irrationalfab

Introducing Live for RubyMotion

Live is a gem for RubyMotion that aims to implement some of the ideas presented in Bret Victor’s Inventing on Principle keynote. It interfaces with the REPL and allows to control it from the comfort of your preferred text editor. The resulting code can then be copied to the source files with minimal adjustments required.

Installation

$ [sudo] gem install motion-live

Usage

Add the following line near the top of your Rakefile:

require 'motion-live'

Run motion-live:

$ rake live

At this point you can just edit LiveScratchpad.rb, hit save, and see the changes being propagated to the application.

By the default only the lines that weren’t present in the previous scratchpad are sent to the REPL. This behaviour is more efficient and well suited for changing the state of the application. However, it is less convenient for redefining logic. In this case is possible to include the #nodiff magic comment, which forces to send the whole file.

An example

$ git clone https://github.com/HipByte/RubyMotionSamples.git
$ cd RubyMotionSamples/Timer

$ vim Rakefile
# Add `require 'motion-live'`

$ vim LiveScratchpad.rb
$ rake live

The timer is a beautiful sample but it is lacking an essential feature: the label does not change color! Lets turbo-charge it.

Add the following code to the scratchpad and save. The method is taken from app/timer_controller.rb.

LiveScratchpad.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#nodiff

class TimerController < UIViewController
  def actionTapped
    if @timer
      # The following line is not present in the original implementation
      @state.textColor = UIColor.greenColor
      @timer.invalidate
      @timer = nil
    else
      # The following line is not present in the original implementation
      @state.textColor = UIColor.redColor
      @duration = 0
      @timer = NSTimer.scheduledTimerWithTimeInterval(0.1, target:self, selector:'timerFired', userInfo:nil, repeats:true)
    end
    @action.selected = !@action.selected?
  end
end

Now, run and stop the timer (without restarting the simulator) and be prepared to blow away your retina.

Known Caveats

  • The code is interacting with REPL through a leaky abstraction. For example, using the nodiff mode in the demo shown in the video would result in new views being created every time the file is saved.
  • Methods and classes which are not actually used by the compiled code might be striped and thus will not be available at runtime.
  • Long declarations might choke the REPL and crash the simulator.
  • This is a 0.1 release.

Source

The project is available at GitHub on the irrationalfab/motion-live repo.

Discussion

Discussion at HackerNews.