This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/ruby | |
result = `/usr/bin/osascript <<EOT | |
tell application "Terminal" | |
activate | |
set Input to display dialog "Gimme some text:" with title "Hello" with icon 0 default answer "" buttons {"OK"} default button 1 | |
return text returned of Input as string | |
end tell | |
EOT | |
` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Module | |
def hookable(method_name) | |
return nil if method_defined?("#{method_name}_without_hooks") | |
alias_method "#{method_name}_without_hooks", method_name | |
metaclass = class << self; self; end | |
hooks = [] | |
define_method(method_name) do |*arguments| | |
hooks.inject(lambda { |*args| send("#{method_name}_without_hooks", *args) }) do |blk, next_blk| |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'benchmark' | |
require 'ostruct' | |
unlocked = 100000.times.map { o = OpenStruct.new; o.timestamp = Time.now - Kernel.rand(10000000); o } ; nil | |
method1 = lambda { | |
achievements_per_day = Hash.new(0) | |
unlocked.each do |achievement| |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
each_slice = (ary, slice_size) -> | |
for i in [0...ary.length] by slice_size | |
ary.slice(i, i + slice_size) | |
console.log each_slice("abcd", 2) | |
console.log each_slice("abcde", 2) | |
console.log each_slice("", 2) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module PartiallyApplicable | |
def apply_partially(new_name, old_name, *fixed_args) | |
define_method(new_name) do |*args| | |
self.send(old_name, *(fixed_args + args)) | |
end | |
end | |
end | |
class MyClass | |
extend PartiallyApplicable |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Instantizable | |
def instantize(name) | |
klass = self | |
define_method(name) do |*args, &block| | |
klass.send(name, self, *args, &block) | |
end | |
end | |
end | |
# Test the module with a normal method, a method with a hash arg, and a method with a block. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
arr = [[{:name=>"one", :enabled=>true}, | |
{:name=>"two", :enabled=>false}], | |
[{:name=>"one", :enabled=>true}, | |
{:name=>"two", :enabled=>true}] | |
] | |
a = arr.flatten.inject(Hash.new(true)) do |result, element| | |
result[element[:name]] &&= element[:enabled] | |
result |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
out_path = ARGV[0] | |
in_path = ARGV[1] | |
glob = "#{in_path}/**/*.coffee" | |
cmd = "coffee -o #{out_path} -c #{in_path}" | |
current_mtime = Time.now |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# note that this is gross | |
findPos = (obj) -> | |
values = while (obj = obj.offsetParent) | |
[obj.offsetLeft, obj.offsetTop] | |
_.reduce(values, (sums, pair) -> | |
left: sums.left + pair[0] | |
right: sums.right + pair[1] | |
left: 0, top: 0) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$.each carousels, -> | |
carousel = $(this) | |
carousels.each -> | |
carousel = $(this) | |
# or abstract it out | |
$.fn.each_j = (callback) -> | |
this.each (i) -> | |
callback.call this, i, $(this) |