<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
	<channel>
		<title><![CDATA[Latest posts for the topic "Ruby Programming with Mentawai (total integration with JRuby)"]]></title>
		<link>http://recipes.mentaframework.org/posts/list/2.page</link>
		<description><![CDATA[Latest messages posted in the topic "Ruby Programming with Mentawai (total integration with JRuby)"]]></description>
		<generator>JForum - http://www.jforum.net</generator>
			<item>
				<title>Ruby Programming with Mentawai (total integration with JRuby)</title>
				<description><![CDATA[ Starting on version 1.13 Mentawai fully supports JRuby. You are able to write your actions in Ruby (or your whole application) or you can call any Ruby code from your Mentawai web application.<br /> <br /> To support plain Ruby, all you need to do is copy the jruby.jar file inside the /WEB-INF/lib directory of your Mentawai web application. However, if you plan to use RubyGems and the Ruby Native Libraries (like 'date' for example) you will need to install JRuby in the machine running your Tomcat web server. To install JRuby is very easy. Follow the steps below:<br /> <br /> :arrow: Download the zip file with the JRuby installation from here: http://dist.codehaus.org/jruby/ (Ex: jruby-bin-1.1.1.zip)<br /> <br /> :arrow: Extract everything inside your root directory (or any directory)<br /> <br /> :arrow: Set the environment variable JRUBY_HOME. (Ex: JRUBY_HOME=c:\jruby-1.1.1)<br /> <br /> :arrow: Set the environment variable JRUBY_OPTS to -rubygems so that you don't need to require 'rubygems' in your code.<br /> <br /> :arrow: Change your PATH environment variable to include %JRUBY_HOME\bin so that you can call the jruby script from anywhere in your shell.<br /> <br /> If you did the above steps correctly, you should be able to play with jruby in your shell. Check the examplew below:<br /> [code]<br /> c:\&gt;jruby -v<br /> ruby 1.8.6 (2008-04-22 rev 6555) [x86-jruby1.1.1]<br /> <br /> c:\&gt;jruby -S gem -v<br /> 1.1.0<br /> <br /> c:\&gt;jruby -S gem list --local<br /> <br /> *** LOCAL GEMS ***<br /> <br /> hoe (1.5.1)<br /> hpricot (0.6)<br /> jruby-openssl (0.2.3)<br /> mechanize (0.7.6)<br /> rake (0.8.1)<br /> rspec (1.1.3)<br /> rubyforge (0.4.5)<br /> sources (0.0.1)<br /> [/code]<br /> <br /> Now let's check an example of how to run Ruby inside your Mentawai web application. (download Mentawai 1.13 [url=http://www.mentaframework.org/beta/mentawai.jar]here[/url] if you need to)<br /> <br /> All your ruby code must be placed inside the /WEB-INF/ruby/ directory of your web application. Mentawai loads ([i]do the require[/i]) any Ruby file from this directory plus it will reload any modified file pretty much like a JSP.<br /> <br /> An action written in Ruby: /WEB-INF/ruby/action_test.rb<br /> [code]<br /> include_class 'org.mentawai.core.BaseAction'<br /> <br /> class MyFirstRubyAction &lt; BaseAction<br />   <br />   def execute<br />     output["msg"] = "Hello from JRuby !!!!"<br />     :success<br />   end<br />   <br /> end<br /> [/code]<br /> <br /> A Java Action calling Ruby code: /WEB-INF/src/org/hello/action/Hello.java<br /> [code]<br /> package org.hello.action;<br /> <br /> import java.util.Iterator;<br /> import java.util.Map;<br /> <br /> import org.mentawai.core.BaseAction;<br /> import org.mentawai.jruby.JRubyInterpreter;<br /> <br /> public class Hello extends BaseAction {<br /> 	<br /> 	JRubyInterpreter ruby = JRubyInterpreter.getInstance();<br /> 	<br /> 	public String execute() {<br /> 		<br /> 		Object users = ruby.eval("u = Users.new");<br /> 		<br /> 		Map map = (Map) ruby.eval("u.get_users");<br /> 		<br /> 		Iterator iter = map.keySet().iterator();<br /> 		<br /> 		StringBuilder sb = new StringBuilder();<br /> 		<br /> 		while(iter.hasNext()) {<br /> 			sb.append(iter.next()).append(" ");<br /> 		}<br /> 		<br /> 		output.setValue("msg", "Hello my friends: " + sb.toString());<br /> 		<br /> 		return SUCCESS;<br /> 	}<br /> }<br /> [/code]<br /> <br /> Ruby code which is called by the Java action above: /WEB-INF/ruby/users_test.rb<br /> [code]<br /> <br /> class Users<br />   <br />   def get_users<br />     a = {}<br />     a['Sergio'] = 1<br />     a['Robert'] = 2<br />     a<br />   end<br />   <br /> end<br /> [/code]<br /> <br /> ApplicationManager:<br /> [code]<br /> package org.hello;<br /> <br /> import org.hello.action.Hello;<br /> <br /> public class ApplicationManager extends org.mentawai.core.ApplicationManager {<br /> 	<br /> 	@Override<br /> 	public void loadActions() {<br /> 		<br /> 		action("/HelloRuby1", Hello.class).on(SUCCESS, "/hello.jsp");<br /> 		<br /> 		ruby("/HelloRuby2", "MyFirstRubyAction").on(SUCCESS, "/hello.jsp");<br /> 		<br /> 	}<br /> 	<br /> }<br /> [/code]<br /> <br /> ]]></description>
				<guid isPermaLink="true">http://recipes.mentaframework.org/posts/preList/57/60.page</guid>
				<link>http://recipes.mentaframework.org/posts/preList/57/60.page</link>
				<pubDate><![CDATA[Thu, 15 May 2008 16:36:28]]> GMT</pubDate>
				<author><![CDATA[ saoj]]></author>
			</item>
	</channel>
</rss>