Showing posts with label jruby. Show all posts
Showing posts with label jruby. Show all posts

Friday, December 7, 2007

Redirecting nginx to Tomcat.

I've got a JRuby on Rails application which is deployed on a Tomcat server. My Http server of choice is nginx. I wanted nginx to redirect one of my domains to the JRuby application. The default port that Tomcat listens on is 8080.

The following nginx configuration snippet solves this problem:

server {
server_name mydomain.com www.mydomain.com;
location / {
proxy_pass http://127.0.0.1:8080;
}
}

BTW, I never thought that my (basic) Russian language skills will be useful in my IT career. Most of the nginx related blog posts I found are in Russian language!

Tuesday, October 16, 2007

How to parse a chess PGN file using JRuby and Chesspresso

I have a lot of chess PGN files with many chess games inside. What I want, is to have some way of filtering this collection e.g. by player names or by opening. Ideally, I'd like to use either Ruby or Python to do that.

My first approach was to find a pure Ruby library and use it. Unfortunately, I couldn't find any good one. No luck with any Python module either. I didn't want to use .Net platform in this case, but I did a quick google search with no good results...

The last hope was the Java platform. This time I was lucky and found Chesspresso. A quick test proved that the library is mature, exposes a very nice API and is actually very fast.
Even though I have a lot of experience with Java language, I don't fancy implementing my projects in statically typed languages. I find Ruby or Python much more readable and testable.

My choice is now clear - JRuby.
include Java
require 'Chesspresso-lib'
include_class 'chesspresso.pgn.PGNReader'

pgn_reader = PGNReader.new 'twic675.pgn'

while game = pgn_reader.parse_game do
if game.white.include?('Shirov')
puts game
end
end
  • The output in my case is:
Shirov,A - Naiditsch,A 1/2-1/2 (2007.10.08)
Shirov,A - Almasi,Z 1-0 (2007.10.09)


deliverhappy.jpg
more funny pictures

Sunday, December 17, 2006

Creating GUI with IronPython, Groovy and JRuby

I was curious what are the differences between the following tools when it comes to creating a GUI desktop application:
  • IronPython (with Windows Forms)
  • Groovy (with Swing)
  • JRuby (with Swing)

I prepared a simple example for each of them. The application shows a form with one button. This button when clicked changes its own text.

IronPython:

tested with 1.1a1 (1.1) on .NET 2.0.50727.42

  • Native windows
  • Possible to pass parameters in the constructor (form = Form(Text="test"))
  • Nice += for event handlers
  • As such a simple example it should work with Mono on Linux, however not sure about more complicated ones.

import clr
clr.AddReference("System.Windows.Forms")
clr.AddReference("System.Drawing")
from System.Windows.Forms
import Application, Button, Form
from System.Drawing import Size


form = Form(
Text="Hello",
Size=Size(200, 200)
)

button = Button(Text="OK!")

def onClick(event, handler):
button.Text = "Pressed!"

button.Click += onClick
form.Controls.Add(button)

Application.Run(form)


Groovy:
tested with 1.0-RC-01
(Thanks to Marcin Domański for his help on this example)
  • Cross-platform
  • Elegant event handlers api (actionPerformed:click)
  • Possible to pass parameters in the constructor (as in IronPython)


import javax.swing.JFrame
import javax.swing.JButton

def frame = new JFrame(
title:"Hello",
size:[200, 200],
defaultCloseOperation:JFrame.EXIT_ON_CLOSE
)

def click = {
event -> event.source.text = "Pressed!"
}

def button = new JButton(
text:"OK!",
actionPerformed:click
)

frame.add(button)
frame.show()



JRuby
tested with 0.9.2
  • Cross-platform
  • JRuby allows you to call Java code using the more Ruby-like underscore_method_naming and to refer to JavaBean properties as attributes.
  • Event handlers using classes is not nice


require 'java'
include_class "javax.swing.JFrame"
include_class "javax.swing.JButton"
include_class "java.awt.event.ActionListener"

frame = JFrame.new("Hello")
frame.set_size(200,200)
frame.defaultCloseOperation = JFrame::EXIT_ON_CLOSE

button = JButton.new("OK!")

class ClickListener < ActionListener
def actionPerformed(event)
event.source.text = "Pressed!"
end
end

button.add_action_listener(ClickListener.new)

frame.add(button)
frame.show


Update: There is a simpler Groovy version with SwingBuilder.