With Mentawai you can write your actions (and your whole application) with Ruby or you can call Ruby code from inside any Java action. it is the best of both languages at your disposal.
Install JRuby:
Go to http://www.jruby.org and install the latest version of JRuby. After that you should be able to do:
[soliveira@sergio-macos ~]$ jruby -v
jruby 1.6.7 (ruby-1.8.7-p357) (2012-02-22 3e82bc8) (Java HotSpot(TM) 64-Bit Server VM 1.6.0_29) [darwin-x86_64-java]
Place your Ruby code inside WEB-INF/ruby:
Mentawai will load and re-load if modified any Ruby code inside this directory.
Writing an action in Ruby:
# file WEB-INF/ruby/hello_ruby.rb include_class 'org.mentawai.core.BaseAction' class HelloRubyAction < BaseAction def hi output["msg"] = "Hello from JRuby !!!!" :success end end
To configure in the application manager:
ruby("/HelloRuby", "HelloRubyAction", "hi") .on(SUCCESS, fwd("/jsp/hello.jsp"));
Calling any Ruby code from a Java action:
Let's define a class in Ruby:
# file WEB-INF/ruby/users.rb class Users def get_users a = {} a['Sergio'] = 1 a['Robert'] = 2 a end end
Now call this Ruby code from Java:
public class HelloAction extends BaseAction { JRubyInterpreter ruby = JRubyInterpreter.getInstance(); @SuppressWarnings("unchecked") public String hi() { Object users = ruby.eval("Users.new"); Map<String, Long> map = (Map<String, Long>) ruby.call(users, "get_users"); Iterator<String> iter = map.keySet().iterator(); StringBuilder sb = new StringBuilder(); while(iter.hasNext()) { String name = iter.next(); long id = map.get(name); sb.append(" ").append(name).append(" (id=").append(id).append(")"); } output.setValue("msg", "Hello my friends:" + sb.toString()); return SUCCESS; } }
Configure in the application manager as usual:
action("/Hello", HelloAction.class, "hi") .on(SUCCESS, fwd("/jsp/hello.jsp"));