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 Object | |
# http://stuff.lilleaas.net/rescue_nil_is_not_awesome | |
def try(*args, &block) | |
return if self.nil? | |
block_given? ? yield(self) : self.__send__(args.shift, *args) | |
end | |
end | |
# or |
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
/** require yo js foo */ | |
function require(url, afterLoaded){ | |
var scriptTag = document.createElement("script"); | |
scriptTag.type = "text/javascript"; | |
if(scriptTag.readyState) { /* IE */ | |
scriptTag.onreadystatechange = function() { | |
if(/^(loaded|complete)/.test(scriptTag.readyState)) { | |
scriptTag.onreadystatechange = null; | |
afterLoaded(); | |
} |
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
*var = 1, 2, 3 #=> [1, 2, 3] | |
*[1, 2, 3] #=> 1, 2, 3 | |
def foo(a, b, c) | |
end | |
foo(%w(1 2 3)) # ArgumentError: wrong number of arguments (1 for 3) | |
foo(*%w(1 2 3)) # ok |
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
#! /bin/bash | |
myen0=`ifconfig en0 | grep "inet " | grep -v 127.0.0.1 | awk '{print $2}'` | |
if [ "$myen0" != "" ] | |
then | |
echo "$myen0" | |
fi | |
myen1=`ifconfig en1 | grep "inet " | grep -v 127.0.0.1 | awk '{print $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
class ::Object | |
# A shorthand for accessing a Classes hidden class | |
# This useful for defining class methods on the fly | |
def metaclass | |
class << self | |
self | |
end | |
end | |
# As a singleton class method to a Class |
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
// how do you initialize a map whose keys are the members of a list? | |
val cats = List('maki, 'unagi, 'shoyu) | |
// #map will return a List of Tuples | |
cats map (_ -> Nil) // List[(Symbol, object Nil)] = List(('maki,List()), ('unagi,List()), ('shoyu,List())) | |
// Map(tuples, _*) | |
Map(cats map (_ -> Nil): _*) // scala.collection.immutable.Map[Symbol,object Nil] = Map('maki -> List(), 'unagi -> List(), 'shoyu -> List()) |
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 Proxy | |
instance_methods.each { |m| undef_method m unless m =~ /(^__|^send$|^object_id$)/ } | |
def initialize(obj) | |
@obj = obj | |
end | |
def sprinkle | |
# adds some sprinkles | |
end |
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
val list = 1 to 4 toList | |
1 to 10 foreach ((_:Any) match { | |
case n: Int if list.exists(_==n) => println("%s -> y" format n) | |
case _ => println("n") | |
}) |
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
select if(somebool, 'true', 'false') from some_table; |