Skip to content

Instantly share code, notes, and snippets.

@fujimura
Created October 17, 2010 14:38
Show Gist options
  • Save fujimura/630910 to your computer and use it in GitHub Desktop.
Save fujimura/630910 to your computer and use it in GitHub Desktop.
enables jquery-like dynamic function adding. ignores closure like 'this' in js.
require 'active_support/all'
class HashWithJavaScriptishAccess < Hash
def method_missing(name, *args)
if name.to_s =~ /=$/
name_to_be_set = name.to_s.gsub(/=$/, '').to_sym
self[name_to_be_set] = args.first
else
self[name] || super
end
end
def curry(name, *args)
fn = self[name]
lambda{fn.bind(self).call *args}
end
end
module JQuerish
def fn
@functions ||= HashWithJavaScriptishAccess.new
end
def method_missing(name, *args)
if @functions.keys.include? name
@functions[name].bind(self).call(*args)
else
super
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment