Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. import javax.script._
  2. import java.io._
  3.  
  4. class Ruby(script:File) extends scala.Dynamic {
  5.   val engine = new ScriptEngineManager().getEngineByName("jruby").asInstanceOf[ScriptEngine with Invocable]
  6.   engine.eval(new FileReader(script));
  7.  
  8.   def language() = "Ruby"
  9.  
  10.   def invokeDynamic(name: String)(args: Any*) =
  11.        engine.invokeFunction(name, args.map(_.asInstanceOf[AnyRef]) : _*)
  12.  
  13.   def typed[T] = error("nope")
  14. }
  15.  
  16. val ruby = new Ruby(new File("Math.rb"))
  17. println(ruby.language) //normal static invocation
  18.  
  19. // Doesn't compile - if we mismatch argument count/type, it does NOT get sent to invoke dynamic
  20. // println(ruby.language(99))
  21.  
  22. //Invoke our ruby methods for doing the math!
  23. assert(5 == ruby.sub(10, ruby.add(2, 3)))
  24.  
  25. //demonstrate undefined methods make it to method-missing
  26. ruby.findByPersonIdAndDepartment("xyz", "acct")