Using RJB to call scala code from Ruby - ruby

I'm about to start rewriting bits and pieces of our Rails app that do the relatively heavier number crunching in Scala for speed. The plan eventually is to have all the processing done by a scala daemon running in the background and just use Rails for the frontend, but in the meantime I want to be able to rewrite bits and pieces of the slower code now and call them synchronously from Ruby.
For this reason I've been looking at RJB (I can't use JRuby) in the hope that I can use it with scala too, seeing as it compiles to Java Bytecode in the end anyway. I've had a google around to see if I can find someone who's already done this or similar but not come up with anything.
My inital, naive attempt was to just compile and package a test scala class into a jar and try loading it using Rjb like this:
Rjb::load("#{Rails.root}/lib/scala/scala-tester-1.0-SNAPSHOT.jar")
MyClass = Rjb::import('com.mydomain.MyClass')
But this just results in a java.lang.NoClassDefFoundError: com/mydomain/MyClass.
I tested importing a standard java class, which works fine, so I tested importing scala.Int, which resulted in another NoClassDefFoundError. This sort of suggests to me that the problem may lie in the scala libraries not being included on the classpath? But it's quite a while since I've had to deal with the endless headaches of Java classpaths so I'm pretty rusty at diagnosing and fixing this kind of problem.
So, has anyone done this? If not, does my hunch sound correct? Any suggestions? Or is there something I'm not thinking of that will mean this approach won't work at all?
EDIT: realised I'd been using slightly incorrect syntax. Have now managed to get it to load my test jar using syntax like this:
Rjb::load(classpath = "#{Rails.root}/lib/scala/scala-tester-1.0-SNAPSHOT.jar", jvmargs=[])
but fail with a different java.lang.NoClassDefFoundError: scala/ScalaObject which suggests it definitely is failing to load the scala libraries.

I got it to work with the following:
RJB_LOAD_PATH = ["#{ENV['SCALA_HOME']}/lib/scala-library.jar", "#{Rails.root}/lib/scala/scala-tester-1.0-SNAPSHOT.jar"].join(File::PATH_SEPARATOR)
RJB_OPTIONS = []
Rjb::load(RJB_LOAD_PATH, RJB_OPTIONS)
Going to put this code in an initializer but it still seems a little hacky and would be glad to hear if anyone has any neater suggestions.

Related

How to debug quickly in phoenix/elixir?

As I am learning Elixir/Phoenix, I happen to run into an issue like this often.
The problem is that this comes from a dependency, so normally I do this:
open deps/something/.../thefile.ex
add some debug code like IO.inspect to see what params are being passed
stop server, recompile with mix deps.recompile something
check the documentation to see what types are expected to that function
compare the types and trace down the problem to my code
This works but I'd like to improve my process, I know I can use Iex to start a repl, but I'd like to get much faster in terms of:
having a repl inside of the error page itself started automatically (like in better_errors gem for Rails)
having a way to see what arguments went into that method without me digging around (could be solved by the former point)
being able to see a method's documentation quickly
Also any other tips are greatly appreciated.
Please open up an issue in Plug: https://github.com/elixir-lang/plug
You have some great suggestions, like accessing the docs and make the arguments explicit. I don't think we can provide a REPL at the place of error though.
EDIT 1: I went ahead and opened an issue here since I got excited about those improvements! :D
EDIT 2: I realize that I should probably have used a comment as this is not quite an answer (yet!)

Cuke4Duke on Grails

Ok, so I've got a grails app here.
On a previous project we used cucumber alongside grails with our step definitions working in ruby.
It was ok, but it would be nice if we could write our defs in groovy, same scripting language we're using everywhere else.
If anyone's ever done it before, or knows how I could get it running, let me know. I've seen a few plugins that don't appear to work with 1.3.7
Is there not some way for me to just put jruby, cuke4duke in the BuildConfig dependencies and just write a scripts/Cucumber.groovy to invoke it?
https://github.com/thetrav/grails-cucumber
Looks like someone else had already built a cuke4duke plugin, my modification causes it to fail on errors and allocates a large permgen space for it to use.
We're now using groovy step definitions (just put .groovy files in ${project_root}/features/step_definitions) and geb to drive the browser, all seems to work ok

How can I find unused methods in a Ruby app?

I have a Ruby app with a lot of classes/modules, and some of them are not used. Is there an easy way of finding out which?
I was thinking to do a profile, and then work with it's output. Any other ideas?
A coverage tool, like rcov might help.
https://github.com/relevance/rcov
As you find methods that are not covered by tests, you should write tests for them or find out if they are used at all.
Removing unused methods is part of refactoring, if you have too many classes that can be a code smell that needs refactored also.
You can put raise (or raise Exception if you don't want it caught) to the start of the suspect method. If nothing breaks, then it might not be in use (either that, or something's catching the exceptions).

What is the ruby test tool called that 'breaks' your code to see how tight your tests are?

A wee while ago I ended up on a page which hosted several ruby tools, which had 'crazy' names like 'mangler' or 'executor' or something. The tool's job was to modify you production code (at runtime) in order to prove that your tests were precise.
Unfortunately I would now like to find that tool again, but can't remember what it was called. Any ideas?
I think you're thinking about Heckle, which flips your code to make sure your tests are accurate. Here:
http://seattlerb.rubyforge.org/heckle/
Maybe you're thinking of the Flay project and related modules:
http://ruby.sadi.st/Ruby_Sadist.html
Also you can try my mutant. Its AST based and currently runs under MRI and RBX in > 2.0 mode. It only has a killer for rspec3, but others are possible also.

Avoiding Groovy/Grails internals while debugging in IntelliJ Idea

I'm using IntelliJ Idea 8.1.2 for Grails development. The dynamic nature of Groovy is giving me a hard time debugging my code: I keep ending up in the internals of Groovy/Grails, i.e. CachedMethod, ExpandoMetaClass or the like.
Is there a way for example to tell the Idea debugger to completely skip the Groovy/Grails internals while debugging and only make steps in my own code? I did notice that there is a configuration option named "Do not step into specific Groovy classes" in Debugger > Groovy but so far I've noticed no difference.
If not, what is your workaround or how do you cope with the situation? Any help would be greatly appreciated.
File->Settings->Debugger->Stepping
Do not step into the classes...
Yeah, I suffer with this annoyance, too, and I don't have a good solution. My workaround once I'm lost in Groovy/Grails code is just to figure out where I should be back in my code, set a break point there, and hit Continue.

Resources