Skip to content

Instantly share code, notes, and snippets.

@sohocoke
Created October 27, 2011 18:35
Show Gist options
  • Save sohocoke/1320400 to your computer and use it in GitHub Desktop.
Save sohocoke/1320400 to your computer and use it in GitHub Desktop.
Handy routines while using InteractiveMacRuby
#== these snippets can go into .irbrc.
# grab an object with its id; in MacRuby this can either be the ruby object id or the decimal / hex pointer address to the object that you can easily find with NSLog / puts.
def o( id )
ObjectSpace._id2ref id
end
# grab instances of a particular class
# e.g. i(MyClass)
def i( klass )
instances = ObjectSpace.each_object(klass).to_a
instances.length > 1 ? instances : instances.first
end
#== these snippets are MacRuby-specific, so should probably go into something like .macirbrc.
# invoke block on main thread; cribbed from the MacRuby mailing list.
def on_main( &block )
o = Object.new
def o.call
@block.call
end
o.instance_variable_set(:@block, block)
o.performSelectorOnMainThread( 'call', withObject: nil, waitUntilDone: true )
end
alias_method :om, :on_main
def concurrently( lambda, completion_lambda = nil )
Dispatch::Queue.concurrent.async do
begin
lambda.call
ensure
completion_lambda.call if completion_lambda
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment