A RubyMotion pretty printer.
instead of using p
or puts
, use mp
to log your debug values to the RubyMotion REPL.
Colors don't show up correctly on github, so these are just examples of what you can do with motion_print
. Try it out for yourself to see the more colorful output!
mp "a string"
"a string"
mp :some_symbol
:some_symbol
mp [1, 'two', :three, 4.0]
[
1,
'two',
:three,
4.0
]
mp({b: "bee", a: 'a', see: 4})
{
a => a,
b => bee,
see => 4
}
Add this line to your application's Gemfile:
Stable:
gem 'motion_print'
Bleeding Edge:
gem 'motion_print', github: 'OTGApps/motion_print'
And then execute:
bundle
motion_print
supports other gems (or even your own classes in your project ) with an opt-in feature. All you have to do is implement the motion_print
method in your class (with option mp
and options
arguments). You can see an example implementation of this in the specs.
def MyCustomClass
def motion_print(mp)
# This will output in red!
mp.colorize("Some Output Here!", :red)
end
end
Here's CDQ's implementation:
class CDQManagedObject
def motion_print(mp, options)
if respond_to? :attributes
"OID: " + mp.colorize(oid.gsub('"',''), options[:force_color]) + "\n" + mp.l_hash(attributes, options)
else
# old colorless method, still more informative than nothing
log
end
end
end
Normally, mp
will not print output when the app is run on the device. To enable output via NSLog, use MotionPrint.enable_nslog
, or the nslog: true
option:
def application(application, didFinishLaunchingWithOptions:launchOptions)
#...
MotionPrint.enable_nslog
#...
# output to NSLog only when you want, like when an error occurs
if some_error_occurred
mp __method__, nslog: true, force_color: :none
mp caller, nslog: true, force_color: none
mp result, nslog: true, force_color: none
end
end
Ruby comes with some great methods for method introspection. These methods look great in motion_print.
mp __method__
will echo the name of the method from which you called motion_print.
mp caller
will trace back up the call stack, so you can see how a method got called.
You can force a color of the output if you want like this:
mp "My String", force_color: :blue # This is usually yellow
or don't output any color:
mp "My String", force_color: :none # This is usually yellow
- Add more core objects people want to output:
UIView
,Struct
, etc. Please open an issue to make suggestions or just implement it yourself and send me a pull request! - Add common gem objects RMQ, ProMotion etc. (CDQ was added and supported)
Test suite.
I used to be a big fan of awesome_print_motion. Then I ran the Instruments allocations profiler on an app that had a big loop with lots of ap
statements. You should have seen the graph increase exponentially over a few seconds. Then I took a look at the actual awesome_print_motion
source code. It's basically a direct port of the awesome_print
gem and just modified to "work" with RubyMotion. There's all kinds of core class extensions and crazy threading things going on... way too complex for my needs to output a pretty version of an object or hash or array.
That, coupled with the fact that the developer of awesome_print_motion doesn't seem responsive to issues or pull requests, I decided to roll my own debugging tool specifically written for RubyMotion development.
awesome_print_motion
is 793 lines of code and extends Array
, Kernel
, Class
, and Object
, and is not tested.
motion_print
is 174 lines of code, adds one method each to String
, and Kernel
, and is fully tested (run rake spec
to see the tests pass).
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request
Mark Rickert (http://otgapps.io)
motion_print is available under the MIT license. See the LICENSE file for more info.