Mix Java and Ruby

So here we are ! To see how awesome it can get to mix Java and Ruby worlds. On this page, we will again use Cucumber to write a scenario, then write some java code , then follow that with some Ruby code and then execute them all together – one happy world isn’t it !

The pre-requisites for this page are that you have completed installation of JRuby and configured it using pik i.e. you have completed the section JRuby Setup.

Agenda:

  1. Download and configure WAC
  2. Write Cucumber scenarios
  3. Write Step Definitions
  4. Download gson.jar and set it in project
  5. Important lines of code explained
  6. Execution and output.
  7. Next

1) Download WAC

For cucumber to show color output on command line, we need to download wac.exe and place it in PATH, in my case , I placed it in c:\jruby-1.7.18\bin. This is required only for JRuby though. The execution is the same, however we would like to see colors “green” and “red” right.

wac_set

 

2) Write Cucumber scenarios

If you are trying to understand this post, it is assumed that you have java programming experience, so I am not going to go into those details here. You can download the feature file from here.

The scenarios are self explanatory as they are in english, cucumber right.

3) Write Step Definitions

You can download the step definition from here

4) Download gson.jar

If you are a java programmer, you already know how to get a certain .jar file. So download the jar and place it in ‘/features/support/jarfile/gson.jar‘. Below is an image to help.

dt_gap height=”10″ /]

gson_jar

After this , add the below lines to env.rb file

5)Execute the scenarios

  1. Open command line
  2. pik list
  3. pik use 171 – or whatever JRuby version you use
  4. bundle exec cucumber features\Mix_Java_Ruby.feature | wac

executed_ruby_java

Next:

That is it Guys ! This was an example to show how both the worlds Java and Ruby can co-exist. We can write elegant Ruby, but can execute it over a jvm, that has been proven mature and enterprise standard for many years.

We are by no means saying that you SHOULD use JRuby, but providing you yet another option.

In our next page , we will execute our framework project code on JRuby