<?xml version="1.0"?>
<rss version="2.0">
<channel>
  <title>Spikes Of Nothingness - Ruby category</title>
  <link>http://weblog.flop.ca/categories/ruby/</link>
  <description>Florin T.PATRASCU&#039;s blog</description>
  <language>en</language>
  <copyright>Florin T.PATRASCU</copyright>
  <lastBuildDate>Wed, 14 Apr 2010 02:17:21 GMT</lastBuildDate>
  <generator>Pebble (http://pebble.sourceforge.net)</generator>
  <docs>http://backend.userland.com/rss</docs>
  
  
  <item>
    <title>extra 5 minutes of fun; JRuby and Velocity</title>
    <link>http://weblog.flop.ca/2009/11/06/1257527741363.html</link>
    
      
        <description>
          &lt;p&gt; As promised, this time I am showing you a very simple way to play with Velocity templating language in JRuby. &lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;blockquote&gt; &lt;strong&gt;jvel.rb&lt;/strong&gt;
&lt;pre&gt;&lt;font color=&#034;#444444&#034;&gt;# A simple JRuby Velocity integration demo &lt;br /&gt;# Notes: &lt;br /&gt;# - save this script as &#039;jvel.rb&#039;.&lt;br /&gt;# - in the folder from where you&#039;re executing this simple script, you&#039;ll&lt;br /&gt;#  have a subfolder: &#039;lib&#039;, containing the Velocity library:&lt;br /&gt;#   lib/velocity-1.6.2-dep.jar&lt;br /&gt;# This jar is intended to be used when you do standalone development&lt;br /&gt;# with Velocity. And assuming that you have jruby in the path, you can &lt;br /&gt;# run the script:&lt;br /&gt;#&lt;br /&gt;#   $ jruby jvel.rb World&lt;br /&gt;#&lt;br /&gt;# You&#039;ll see: Hello World!&lt;br /&gt;# ----------- &lt;br /&gt;&lt;br /&gt;&lt;/font&gt;&lt;font color=&#034;#a52a2a&#034;&gt;&lt;strong&gt;require&lt;/strong&gt;&lt;/font&gt; &lt;font color=&#034;#008000&#034;&gt;&#039;java&#039;&lt;/font&gt;   &lt;br /&gt;&lt;br /&gt;Dir&lt;font color=&#034;#4444ff&#034;&gt;&lt;strong&gt;[&lt;/strong&gt;&lt;/font&gt;&lt;font color=&#034;#008000&#034;&gt;&amp;quot;lib/*.jar&amp;quot;&lt;/font&gt;&lt;font color=&#034;#4444ff&#034;&gt;&lt;strong&gt;]&lt;/strong&gt;&lt;/font&gt;.each &lt;font color=&#034;#4444ff&#034;&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt; |jar| &lt;font color=&#034;#a52a2a&#034;&gt;&lt;strong&gt;require&lt;/strong&gt;&lt;/font&gt; jar &lt;font color=&#034;#4444ff&#034;&gt;&lt;strong&gt;}&lt;br /&gt;&lt;/strong&gt;&lt;/font&gt;&amp;nbsp;&lt;br /&gt;&lt;strong&gt;class&lt;font color=&#034;#2040a0&#034;&gt;&lt;strong&gt; JRubyVelocity&lt;/strong&gt;&lt;/font&gt;&lt;/strong&gt;   &lt;br /&gt;    include_class &lt;font color=&#034;#008000&#034;&gt;&#039;java.io.StringWriter&#039;&lt;/font&gt;   &lt;br /&gt;    include_class &lt;font color=&#034;#008000&#034;&gt;&#039;org.apache.velocity.app.Velocity&#039;&lt;/font&gt;   &lt;br /&gt;    include_class &lt;font color=&#034;#008000&#034;&gt;&#039;org.apache.velocity.VelocityContext&#039;&lt;/font&gt;    &lt;strong&gt;&lt;br /&gt;&lt;br /&gt;  def&lt;font color=&#034;#ff0000&#034;&gt; render&lt;/font&gt;&lt;/strong&gt; &lt;font color=&#034;#4444ff&#034;&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt; arg&lt;font color=&#034;#4444ff&#034;&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt; &lt;br /&gt;    Velocity.init&lt;br /&gt;    writer &lt;font color=&#034;#4444ff&#034;&gt;=&lt;/font&gt; StringWriter.new  &lt;br /&gt;    ctx &lt;font color=&#034;#4444ff&#034;&gt;=&lt;/font&gt; VelocityContext.new&lt;font color=&#034;#4444ff&#034;&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt; &lt;font color=&#034;#4444ff&#034;&gt;&lt;strong&gt;{&lt;/strong&gt;&lt;/font&gt;&lt;font color=&#034;#008000&#034;&gt;&#039;name&#039;&lt;/font&gt; &lt;font color=&#034;#4444ff&#034;&gt;=&amp;gt;&lt;/font&gt; arg&lt;font color=&#034;#4444ff&#034;&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;/font&gt;&lt;font color=&#034;#4444ff&#034;&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;                      &lt;br /&gt;    Velocity.evaluate&lt;font color=&#034;#4444ff&#034;&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt; ctx&lt;font color=&#034;#4444ff&#034;&gt;,&lt;/font&gt; writer&lt;font color=&#034;#4444ff&#034;&gt;,&lt;/font&gt; &lt;font color=&#034;#008000&#034;&gt;&amp;quot;log&amp;quot;&lt;/font&gt;&lt;font color=&#034;#4444ff&#034;&gt;,&lt;/font&gt; &lt;font color=&#034;#008000&#034;&gt;&amp;quot;Hello ${name}!&amp;quot;&lt;/font&gt;&lt;font color=&#034;#4444ff&#034;&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt; &lt;br /&gt;    &lt;strong&gt;return&lt;/strong&gt; writer.getBuffer&lt;br /&gt;  &lt;strong&gt;end&lt;br /&gt;&lt;/strong&gt;&lt;strong&gt;end&lt;/strong&gt;  &lt;br /&gt;&lt;br /&gt;&lt;font color=&#034;#a52a2a&#034;&gt;&lt;strong&gt;puts&lt;/strong&gt;&lt;/font&gt; JRubyVelocity.new&lt;font color=&#034;#4444ff&#034;&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt;&lt;font color=&#034;#4444ff&#034;&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;.render&lt;font color=&#034;#4444ff&#034;&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/font&gt; &lt;font color=&#034;#2040a0&#034;&gt;&lt;strong&gt;ARGV&lt;/strong&gt;&lt;/font&gt;.pop&lt;font color=&#034;#4444ff&#034;&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/font&gt;  &lt;/pre&gt;
&lt;/blockquote&gt;   &lt;br /&gt;
Good luck!
        </description>
      
      
    
    
    
    <category>Ruby</category>
    
    <category>Java</category>
    
    <comments>http://weblog.flop.ca/2009/11/06/1257527741363.html#comments</comments>
    <guid isPermaLink="true">http://weblog.flop.ca/2009/11/06/1257527741363.html</guid>
    <pubDate>Fri, 06 Nov 2009 17:15:41 GMT</pubDate>
  </item>
  
  <item>
    <title>5 minutes of fun: JRuby and StringTemplate</title>
    <link>http://weblog.flop.ca/2009/11/04/1257357667790.html</link>
    
      
        <description>
          &lt;p&gt; If you like to explore some stuff on templating, and I highly recommend &lt;a href=&#034;http://www.stringtemplate.org/&#034;&gt;StringTemplate&lt;/a&gt;, this is how you can do it from &lt;a href=&#034;http://jruby.org/&#034;&gt;JRuby&lt;/a&gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;/p&gt;
&lt;blockquote&gt;
&lt;h1&gt;jrst.rb&lt;/h1&gt;
&lt;pre&gt;&lt;em&gt;&lt;font color=&#034;#b22222&#034;&gt;# A simple JRuby StringTemplate integration demo &lt;br /&gt;&lt;/font&gt;&lt;/em&gt;&lt;em&gt;&lt;font color=&#034;#b22222&#034;&gt;# Notes: &lt;br /&gt;&lt;/font&gt;&lt;/em&gt;&lt;em&gt;&lt;font color=&#034;#b22222&#034;&gt;# - save this script as &#039;jrst.rb&#039;.&lt;br /&gt;&lt;/font&gt;&lt;/em&gt;&lt;em&gt;&lt;font color=&#034;#b22222&#034;&gt;# - in the folder from where you&#039;re executing this simple script, you&#039;ll&lt;br /&gt;&lt;/font&gt;&lt;/em&gt;&lt;em&gt;&lt;font color=&#034;#b22222&#034;&gt;#  have a subfolder: &#039;lib&#039;, containing the StringTemplate libraries:&lt;br /&gt;&lt;/font&gt;&lt;/em&gt;&lt;em&gt;&lt;font color=&#034;#b22222&#034;&gt;#    lib/antlr-2.7.7.jar&lt;br /&gt;&lt;/font&gt;&lt;/em&gt;&lt;em&gt;&lt;font color=&#034;#b22222&#034;&gt;#    lib/stringtemplate-3.2.1.jar&lt;br /&gt;&lt;/font&gt;&lt;/em&gt;&lt;em&gt;&lt;font color=&#034;#b22222&#034;&gt;# run the script&lt;br /&gt;&lt;/font&gt;&lt;/em&gt;&lt;em&gt;&lt;font color=&#034;#b22222&#034;&gt;# $ jruby jrst.rb World&lt;br /&gt;&lt;/font&gt;&lt;/em&gt;&lt;em&gt;&lt;font color=&#034;#b22222&#034;&gt;#&lt;br /&gt;&lt;/font&gt;&lt;/em&gt;&lt;em&gt;&lt;font color=&#034;#b22222&#034;&gt;# You&#039;ll see: Hello World!&lt;br /&gt;&lt;/font&gt;&lt;/em&gt;&lt;em&gt;&lt;font color=&#034;#b22222&#034;&gt;# ----------- &lt;br /&gt;&lt;/font&gt;&lt;/em&gt;&lt;strong&gt;&lt;font color=&#034;#5f9ea0&#034;&gt;require&lt;/font&gt;&lt;/strong&gt; &lt;strong&gt;&lt;font color=&#034;#bc8f8f&#034;&gt;&#039;java&#039;&lt;/font&gt;&lt;/strong&gt;   &lt;br /&gt;&lt;br /&gt;Dir[&lt;strong&gt;&lt;font color=&#034;#bc8f8f&#034;&gt;&amp;quot;lib/*.jar&amp;quot;&lt;/font&gt;&lt;/strong&gt;].each { |jar| &lt;strong&gt;&lt;font color=&#034;#5f9ea0&#034;&gt;require&lt;/font&gt;&lt;/strong&gt; jar }&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;&lt;font color=&#034;#a020f0&#034;&gt;class&lt;/font&gt;&lt;/strong&gt; JRubySringTemplate&lt;br /&gt;  include_class &lt;strong&gt;&lt;font color=&#034;#bc8f8f&#034;&gt;&#039;java.io.StringWriter&#039;&lt;/font&gt;&lt;/strong&gt;   &lt;br /&gt;  include_class &lt;strong&gt;&lt;font color=&#034;#bc8f8f&#034;&gt;&#039;org.antlr.stringtemplate.StringTemplate&#039;&lt;/font&gt;&lt;/strong&gt;    &lt;br /&gt;&lt;br /&gt;  &lt;strong&gt;&lt;font color=&#034;#a020f0&#034;&gt;def&lt;/font&gt;&lt;/strong&gt; &lt;strong&gt;&lt;font color=&#034;#0000ff&#034;&gt;render &lt;/font&gt;&lt;/strong&gt;( arg)                         &lt;br /&gt;    st = StringTemplate.new(&lt;strong&gt;&lt;font color=&#034;#bc8f8f&#034;&gt;&amp;quot;Hello $name$!&amp;quot;&lt;/font&gt;&lt;/strong&gt;)&lt;br /&gt;    st.set_attributes( {&lt;strong&gt;&lt;font color=&#034;#bc8f8f&#034;&gt;&#039;name&#039;&lt;/font&gt;&lt;/strong&gt; =&amp;gt; arg});&lt;br /&gt;    writer = StringWriter.new   &lt;br /&gt;    writer.write( st.toString)&lt;br /&gt;    &lt;strong&gt;&lt;font color=&#034;#a020f0&#034;&gt;return&lt;/font&gt;&lt;/strong&gt; writer.getBuffer&lt;br /&gt;  &lt;strong&gt;&lt;font color=&#034;#a020f0&#034;&gt;end&lt;/font&gt;&lt;/strong&gt; &lt;strong&gt;&lt;font color=&#034;#a020f0&#034;&gt;&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;&lt;/font&gt;&lt;/strong&gt;puts JRubySringTemplate.new().render( ARGV[0]) &lt;/pre&gt;
&lt;/blockquote&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt; My 5 minutes of fun in a lunch break. Next time I&#039;ll show you how to use Velocity in the same way :)&lt;br /&gt;
&lt;br /&gt;
Have fun! &lt;/p&gt;
        </description>
      
      
    
    
    
    <category>Ruby</category>
    
    <category>Java</category>
    
    <comments>http://weblog.flop.ca/2009/11/04/1257357667790.html#comments</comments>
    <guid isPermaLink="true">http://weblog.flop.ca/2009/11/04/1257357667790.html</guid>
    <pubDate>Wed, 04 Nov 2009 18:01:07 GMT</pubDate>
  </item>
  
  <item>
    <title>JRuby actions in JPublish</title>
    <link>http://weblog.flop.ca/2007/07/17/1184705235053.html</link>
    
      
        <description>
          &lt;p&gt;  &lt;/p&gt;
As some of you already know, &lt;a href=&#034;http://code.google.com/p/jpublish/&#034;&gt;JPublish&lt;/a&gt; can execute &lt;a href=&#034;http://code.google.com/p/jpublish/wiki/Actions&#034;&gt;actions&lt;/a&gt; written in &lt;a href=&#034;http://jruby.codehaus.org/&#034;&gt;JRuby&lt;/a&gt;. This is all possible through the magic of the &lt;a href=&#034;http://www.headius.com/jrubywiki/index.php/Java_Integration&#034;&gt;BSF&lt;/a&gt; :)&lt;br /&gt;
&lt;br /&gt;
I received a lot of questions about how can a JRuby Action use the java objects available in the &lt;a href=&#034;http://code.google.com/p/jpublish/wiki/Context&#034;&gt;JPublishContext&lt;/a&gt;?&lt;br /&gt;
&lt;br /&gt;
There&#039;s nothing simpler than that, in JPublish is easy :) Below is the body of a JRuby Action which will print a message to the designated console log and will populate the page context with the variable &amp;quot;hello&amp;quot;. The variable &amp;quot;hello&amp;quot; will be accessible to Velocity or Freemarker during the post action execution rendering process.&lt;br /&gt;
&lt;br /&gt;
&lt;blockquote&gt;&lt;font color=&#034;#333399&#034;&gt;&lt;em&gt;&lt;strong&gt;&lt;font color=&#034;#000000&#034;&gt;HelloJRuby.rb&lt;/font&gt;&lt;/strong&gt;&lt;/em&gt;&lt;br /&gt;
&lt;br /&gt;
require &#039;java&#039;&lt;br /&gt;
$syslog.info(&amp;quot;&lt;/font&gt;&lt;font color=&#034;#333399&#034;&gt;HelloJRuby&lt;/font&gt;&lt;font color=&#034;#333399&#034;&gt;.rb action ...&amp;quot;)&lt;br /&gt;
$context.put(&amp;quot;hello&amp;quot;, &amp;quot;Hello World!&amp;quot;)&lt;br /&gt;
&lt;/font&gt;&lt;/blockquote&gt;&lt;br /&gt;
Simple, eh? Use the dollar sign in front of the variables you expect to find in the context.&lt;br /&gt;
&lt;br /&gt;
Also, there is a recent web site with a lot of JRuby news and tips, check it out in case you&#039;re interested. Is called &lt;a href=&#034;http://www.jrubyinside.com/&#034;&gt;JRuby Inside&lt;/a&gt;.&lt;br /&gt;
&lt;br /&gt;
Cheers!
        </description>
      
      
    
    
    
    <category>Ruby</category>
    
    <category>Java</category>
    
    <comments>http://weblog.flop.ca/2007/07/17/1184705235053.html#comments</comments>
    <guid isPermaLink="true">http://weblog.flop.ca/2007/07/17/1184705235053.html</guid>
    <pubDate>Tue, 17 Jul 2007 20:47:15 GMT</pubDate>
  </item>
  
  </channel>
</rss>
