Skip to content

Instantly share code, notes, and snippets.

View softprops's full-sized avatar
®️
Rustling

Doug Tangren softprops

®️
Rustling
View GitHub Profile
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
/** 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();
}
*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
#! /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}'`
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
// 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())
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
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")
})
select if(somebool, 'true', 'false') from some_table;