Skip to content

Instantly share code, notes, and snippets.

@gunn
gunn / iplay.rb
Created March 1, 2011 01:14
Play any track visible to itunes - local or in shared library with macruby.
#! /bin/env macruby
framework 'Foundation'
framework 'ScriptingBridge'
itunes = SBApplication.applicationWithBundleIdentifier("com.apple.itunes")
itunes.run
itunes.sources.each_with_index do |source, lib_index|
library = source.libraryPlaylists.first
@gunn
gunn / time.rb.js
Created February 25, 2011 13:24
It turns out javascript is actually pretty flexible. If only we had __noSuchProperty__.
$rubyMode = false
// Lets us define a named method on any object either as a getter
// or a traditional function. Getters are fun because they look more like ruby.
Object.prototype.defineMethod = function(name, method){
if ($rubyMode) this.__defineGetter__(name, method);
else this[name] = method;
}
// Enable (5).times with argument |number|
@gunn
gunn / alias_underscore.rb
Created February 12, 2011 21:44
Use method_missing to allow camel-cased methods to be called by equivalent underscored names. Written with macruby in mind. Note that #respond_to? should be updated too.
# Faster method, the regex / gsub overhead is invoked only the first time, then an alias is used. I'm not sure that this wouldn't introduce side-effects however. What happens when the original camel-cased method is changed / removed, but it's alias remains inplace?
class Object
alias orig_method_missing method_missing
def method_missing name, *args, &block
if name =~ /\A[a-z\d_]+\z/
camel_name = name.gsub(/_(.)/) { $1.upcase }
if self.respond_to? camel_name
# The 'rescue nil' is because macruby seems to do some odd things with classes sometimes (SBElementArray?):
self.class.send :alias_method, name, camel_name rescue nil
@gunn
gunn / directory_web_service.rb
Created January 29, 2011 10:44 — forked from anonymous/directory_web_service.rb
Refactor of some code from a blog post.
require 'mechanize'
require 'nokogiri'
require 'sinatra'
get '/:psuid' do
find_name params[:psuid]
end
def find_name(psuid)
@gunn
gunn / singleton_methods.rb
Created January 15, 2011 09:34 — forked from samnang/singleton_methods.rb
A large executable list of methods of define singleton methods in ruby
##############################################
# Different ways to define singleton methods #
##############################################
# Being able to reference external instance variables when creating a method
# enables useful meta-programming techniques in ruby. The styles which are
# capable of this will define methods that "Moof!" (like Clarus the Dogcow).
moof = "Moof!"
@gunn
gunn / data_attributes.rb
Created January 14, 2011 10:40
Methods of creating data attributes in haml.
require "rubygems"
require "haml"
class Symbol
def -(string)
"#{self}-#{string}".intern
end
end
def method_missing name, *args, &block
@gunn
gunn / routes.rb
Created January 14, 2011 09:30 — forked from anonymous/gist:779401
Help for an IRC user
Blog::Application.routes.draw do
resources :posts do
resources :comments
end
# You can have the root of your site routed with "root"
# just remember to delete public/index.html.
root :to => "home#index"
end
@gunn
gunn / crash_messages.txt
Created December 15, 2010 03:59
MacRuby crash report
"\"\x00.\x00t\x00r\x00a\x00c\x00k\x00e\x00d\x00 \x00f\x00i\x00l\x00e\x00n\x00a\x00m\x00e\x00s\x00.\x00p\x00l\x00i\x00s\x00t\x00\"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\".tracked filenames.plist\"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
/Users/Arthur/macruby_segfault_test/segfault.rb:8:in `block': failed to allocate memory (NoMemoryError)
from /Users/Arthur/macruby_segfault_test/segfault.rb:8:in `<main>'
[BUG] destination 0x2000228a8 isn't in the auto zone
MacRuby 0.9 (ruby 1.9.2) [universal-darwin10.0, x86_64]
Abort trap
@gunn
gunn / beautiful.rb
Created December 10, 2010 01:51
Some common but neat ruby tricks
class Symbol
def to_proc
proc { |obj, *args| obj.send(self, *args) }
end
end
class Array
def sum
inject :+
end
@gunn
gunn / application_helper.rb
Created November 17, 2010 02:45
Helper to load from Google CDN but with fallbacks
def javascript_fallback primary, fallback, test
html = javascript_include_tag( primary )
html << "\n" << content_tag(:script, :type => "text/javascript") do
%Q{
if (#{test}) {
document.write(unescape("%3Cscript src='#{fallback}' type='text/javascript'%3E%3C/script%3E"));
}
}.gsub(/^ {6}/, '')
end
html