Examples for a blog post I'm writing on creating arbitrary datatypes.
This file contains hidden or 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
test = (message, fn) -> throw "\"#{message}\" Failed" unless do fn | |
equal = (obj1, obj2) -> | |
[type_1, type_2] = [typeof obj1, typeof obj2] | |
return false unless type_1 is type_2 | |
if type_1 isnt "object" | |
return obj1 is obj2 | |
else | |
if obj1 instanceof Array | |
if obj2 instanceof Array |
This file contains hidden or 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 = (arr, func, index=0) -> | |
if index < arr.length then [ func(arr[index], index), each(arr, func, index + 1)... ] else [] | |
camelize = (input) -> | |
pieces = input.split(/[\W_-]/) | |
each(pieces, capitalize).join("") | |
capitalize = (input) -> | |
input.charAt(0).toUpperCase() + input.slice(1) |
This file contains hidden or 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 = (arr, func, index=0) -> | |
if index < arr.length then [ func(arr[index], index), each(arr, func, index + 1)... ] else [] |
This file contains hidden or 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
# MacPorts Installer addition on 2011-03-31_at_12:54:10: adding an appropriate PATH variable for use with MacPorts. | |
#export PATH=/opt/local/bin:/opt/local/sbin:/usr/local/bin:/usr/local/sbin:/usr/local/mysql/bin:$PATH | |
# Finished adapting your PATH environment variable for use with MacPorts. | |
export PATH=/usr/local/bin:/usr/local/sbin:/usr/local/mysql/bin:$PATH | |
[[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm" |
This file contains hidden or 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
jQuery -> | |
count = 0 | |
$("a").click (e) -> | |
e.preventDefault() | |
alert("Hello World #{++count}") |
This file contains hidden or 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 ProofTest | |
class Model | |
def self.attr_reader property | |
define_method property do | |
puts "reading #{attributes[property]} from #{property}" | |
attributes[property] | |
end | |
end | |
def self.attr_writer property |
This file contains hidden or 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 ObservableAttributes | |
module Observers | |
def observers | |
# A little hack to prevent the need for an @observers instance var | |
class << self | |
@observers ||= Hash.new | |
end | |
end | |
This file contains hidden or 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
env = {"REQUEST_METHOD" => "GET", | |
"PATH_INFO" => "/demo/index" | |
"rack.input" => StringIO.new | |
} | |
ActionController.new.call(env).last.body |
This file contains hidden or 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
# some credit to https://github.com/maddox/magick-installer | |
require 'formula' | |
def ghostscript_srsly? | |
ARGV.include? '--with-ghostscript' | |
end | |
def ghostscript_fonts? | |
File.directory? "#{HOMEBREW_PREFIX}/share/ghostscript/fonts" | |
end |