Skip to content

Instantly share code, notes, and snippets.

@ddrscott
ddrscott / any_controller.rb
Created January 25, 2013 17:51
How to free up the ns_data (NSMutableData) object? Even though I'm aways overwriting the @image_view.image, resources are not getting freed. If I try calling CG*Release methods, I get other crashes. Every to on_click_test_a is called 60 mb will be leaked.
def viewWillAppear
unless @image_view
@image_view = UIImageView.alloc.initWithFrame([[0,0],[200,200]])
@image_view.contentMode = UIViewContentModeScaleAspectFit
@image_view.backgroundColor = UIColor.blackColor
@image_view.center = view.center
view.addSubview @image_view
if self.navigationController
@test_a_button = UIBarButtonItem.alloc.initWithTitle('Test', style: UIBarButtonItemStylePlain, target: self, action: :on_click_test_a)
@ddrscott
ddrscott / QuickDialog-0.7.podspec
Last active December 13, 2015 16:59
RubyMotion parser crash. env => OSX 10.8.2, RubyMotion 1.32, Xcode 4.6
Pod::Spec.new do |s|
s.name = 'QuickDialog'
s.version = '0.7'
s.platform = :ios
s.license = 'Apache License, Version 2.0'
s.summary = 'Quick and easy dialog screens for iOS.'
s.homepage = 'http://escoz.com/quickdialog'
s.author = { 'Eduardo Scoz' => 'contact@escoz.com' }
s.source = { :git => 'https://github.com/escoz/QuickDialog.git', :tag => '0.7' }
@ddrscott
ddrscott / lifo_queue.rb
Created February 14, 2013 21:03
A simple last in - first out queue for RubyMotion. I feel like it should be harder than this...
class LifoQueue
def initialize(name, depth)
raise ArgumentError, "depth must be a number greater than zero" unless Fixnum === depth and depth > 0
super
@depth = depth
@stack = []
@mutex = Mutex.new
@queue = Dispatch::Queue.send :new, name
#!/usr/bin/ruby
printf "\033[0mAll attributes off\033[0m\n"
printf "\033[1mBold\033[0m\n"
printf "\033[4mUnderline\033[0m\n"
printf "\033[5mBlink\033[0m\n"
printf "\033[8mHide\033[0m\n"
printf "\033[30mBlack\033[0m\n"
printf "\033[31mRed\033[0m\n"
printf "\033[32mGreen\033[0m\n"
printf "\033[33mYellow\033[0m\n"
@ddrscott
ddrscott / Rakefile
Created February 26, 2013 19:39 — forked from adelevie/Rakefile
app.libs << '/usr/lib/libz.1.1.3.dylib'
app.libs << '/usr/lib/libsqlite3.dylib'
app.frameworks += [
'AudioToolbox',
'Accounts',
'AdSupport',
'CFNetwork',
'CoreGraphics',
'CoreLocation',
'MobileCoreServices',
@ddrscott
ddrscott / parse_test.mm
Last active December 14, 2015 07:19
Using Parse.com API, how to save and retrieve an object in offline mode?
- testOffline {
// Important! do all methods without Network
PFObject *todo1 = [PFObject objectWithClassName:@"Todo"];
[todo1 setObject:@"Learn Parse API" forKey:@"text"];
[todo1 saveEventually];
PFObject *todo2 = [PFObject objectWithClassName:@"Todo"];
[todo2 setObject:@"Implement iOS" forKey:@"text"];
[todo2 saveEventually];
@ddrscott
ddrscott / layouts.rb
Last active December 14, 2015 23:08
RubyMotion UIBarButtonItem Helper
# Example:
# self.navigationItem.leftBarButtonItems = [
# ui_bar_button_item('1'){App.alert('One')},
# ui_bar_button_item('2'){App.alert('Two')}
# ]
def ui_bar_button_item(title, &block)
item = UIBarButtonItem.alloc.initWithTitle(title, style: UIBarButtonItemStylePlain, target: block, action: :call)
item.instance_variable_set('@_target', block) # retain
item
end
@ddrscott
ddrscott / moon_spinner.rb
Created April 8, 2013 18:13
Rotating phases of the moon using Emoji.
# -*- coding: utf-8 -*-
# phases = %w{🌑 🌒 🌓 🌔 🌕 🌖 🌗 🌘}
phases = (0x1F311..0x1F318).to_a.collect{|c| [c].pack('U*')}
clear_line = "\r\e[0K"
1000.times do |i|
phase = phases[i % phases.size]
print "#{clear_line} #{phase}\t#{i} #{phase.unpack('U*').map{ |i| "\\u" + i.to_s(16).rjust(4, '0') }.join}"
sleep rand / 10.0
@ddrscott
ddrscott / xe_scrape.rb
Created April 19, 2013 17:49
scrape xe.com using Ruby and Nokogiri
api_url = "http://www.xe.com/currencytables/?from=USD&date=#{Time.now.strftime('%Y-%m-%d')}"
doc = Nokogiri(open(api_url))
rows = doc.search('#historicalRateTbl tr')
rows.shift # disgard header
currencies = {}
while row = rows.shift
currency = row.children[0].inner_text
rate_to_us = row.children[2].inner_text
# rate_from_us = row.children[3].inner_text
@ddrscott
ddrscott / letters.rb
Created April 15, 2014 05:18
My son wanted to see colorful letters. This is what I came up with. He actually stayed awake as I type it. It was originally a one liner done in IRB.
az=("A".."z").to_a.map {|m|
"\033[38;5;#{rand(180)+16}m#{m}\033[0m" # wrap char in random color
}
loop {
puts [az*" "] * 25 # fill some of the screen
az<<(az.shift) # rotate chars
sleep(0.3) # slow down
puts "\e[26F" # cursor up 26 lines
}