Skip to content

Instantly share code, notes, and snippets.

tw@hernan-cortes:~$ [master*] time rake jasmine:headless
....................................
PASS: 36 tests, 0 failures, 0.175 secs.
real 0m4.303s
user 0m2.660s
sys 0m0.900s
require 'rubygems'
require 'rspec'
module EventLogging
def self.included(base)
base.extend ClassMethods
end
module ClassMethods
@cv
cv / graceful_degradation_helper.rb
Created June 29, 2011 21:02
a graceful degradation helper for dangerous operations in the template
module GracefulDegradationHelper
# doctest:
#
# >> gracefully { tag(:hello) }
# => "<hello />"
#
# >> gracefully { raise "oops" }
# => ""
#
namespace :foo do
task :a do
exec "echo A"
end
task :b => [:'foo:a'] do
exec "echo B"
end

Pimp

The following code shows the Pimp language output for some specific input, showing some of the benefits from using such a nice language.

Defining something

 an Account:owner, amount

is positive?

*.js
iik> java:util:ArrayList new
*** - java.lang.IllegalArgumentException: object is not an instance of declaring class (Condition Error JavaException)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
java.lang.reflect.Method.invoke(Method.java:585)
ioke.lang.JavaMethodJavaMethod.activate(JavaMethodJavaMethod.java:45)
ioke.lang.JavaMethodJavaMethod.activate(JavaMethodJavaMethod.java:38)
ioke.lang.IokeObject.activate(IokeObject.java:932)
Refactoring = Origin mimic do(
ifToUnless = method("Returns a message with all the if statements converted to unless statements", message,
unless(message kind == "Message", return)
message each(i,
Refactoring ifToUnless(i next)
i arguments each(j,
Refactoring ifToUnless(j)
)
)
use("ispec")
describe("Java",
it("should create new methods on an object",
Java compileMethod(Text, :disemvowel, #[{
return runtime.newText(Text.getText(on).replaceAll("a|e|i|o|u|A|E|I|O|U", ""));
}])
"hello, world!" disemvowel should == "hll, wrld!"
; I'm assuming the Java namespace will have a bunch of interesting utilities for Java interop - naming might change.
Java method = dsyntax("",
[javaCode]
; use ASM some other bytecode writing API to create a child of JavaMethod or JavaMethod.WithNoArguments, load it and return it
)
Text lower = Java method("Returns a lower case version of this text",
public Object activate(IokeObject method, IokeObject context, IokeObject message, Object on) throws ControlFlow {
getArguments().getValidatedArgumentsAndReceiver(context, message, on, new ArrayList<Object>(), new HashMap<String, Object>());