String generalization called from JRuby

Forking the discussion a bit, but I'm interested in your use of JRuby here.
It seems like a great idea to have little snippits of code that are easier
to understand and maintain in Ruby, and be able to call them from Java with
JRuby. Are you doing this already, or is this a first possible attempt at
it?

(I ask because I'm on the JRuby team)

···

On 4/6/06, Peter Szinek <peter@rt.sk> wrote:

Hello all,

I am having the following problem: I have to implement a method
which accepts a string and returns a generalized form of the string.
Some examples:

'12345' -> \d+
'ABCDE' -> [A-Z]+
'john.smith@my.super.server.rb' -> [a-z]+\.[a-z]+@([a-z]+\.)+[a-z]
'123-45-678-90' -> (\d+-)+\d+
'item:' -> [a-z]+:
'Peter, SZINEK': -> [A-Z][a-z]+, [A-Z]+
'http://www.google.com' -> [a-z]+://([a-z]+\.)+[a-z]
'jd£;d:L348kddd3' -> .*

So, given an example, the function should generate a generic regexp
which is then used to match the instances of the same class (i.e. based
on an e-mail, you create a regexp which can be used to match other
emails).
Of course this problem is not solvable in general. You would need more
examples (both positive and negative ones, as it is proven that just
using positive examples you can not generalize a pattern (generate a
regular grammar desribibg it) and then machine learn/induce a
grammar/etc based on that. Unfortunately i have only one positive
example. So i do not need a perfect solution (which is not possible
anyway) just a mostly working one.

I have implemented it in java, and it works pretty well but it is ugly
as hell (as any java code dealing with stuff where you need regexps,
slicing, maps etc). Any ideas for a nice Ruby code solving this? Then i
would call it via JRuby (and killed by the other colleagues using java
but not Ruby ;-).

thx,
Peter

--
Charles Oliver Nutter @ headius.blogspot.com
JRuby Developer @ jruby.sourceforge.net
Application Architect @ www.ventera.com

Howdy,

Charles O Nutter wrote:

Forking the discussion a bit, but I'm interested in your use of JRuby here.
It seems like a great idea to have little snippits of code that are easier
to understand and maintain in Ruby, and be able to call them from Java with
JRuby. Are you doing this already, or is this a first possible attempt at
it?

Well, this will be my first attempt at JRuby - but this should not mean anything, as i begun to work with Ruby 2 weeks ago :wink: Nevertheless i can answer your question: I did this with Jython and i liked it. (I do not really like to code in java, (i prefer dynamically typed languages over statically typed ones) i am just doing it because i am working on a big project which is written in java.) So as soon as i can (and it makes sense) i am rushing to Jython/Jruby. (actually i like Ruby much more after 2 weeks than Python after 2 years :wink: So i will go with JRuby in the future. Actually JRuby is a clear winner in the compatibility issue imho (against Jython, which is faaaar from being Python2.4 compatible)

I think it is absolutely cool to do everything in JRuby which makes sense (i.e. to open a file, i will not go to JRuby - although after 5 years as a Java professional i have to still look up the doc on how to open a file every time - not kidding here - so maybe this is not a bad idea either :wink: but operations which require string manipulation, regexps, lists, hashes, slicing, metaprogramming, can leverage blocks etc just to name a few are defintely ideal to be "outsorced" to JRuby.
Another ideal case is when the input and the output are both something simple (e.g. String -> String) - i.e. they are not depending heavily on non-built-in java objects.

My only problem is, that my other colleagues (and the company in general) is java only (although i am heavily sowing the seeds of Ruby also here :wink: and thus they do not really like the idea that someday they could possibly have to maintain Ruby code. So i am infiltrating our codebase slowly (and hope to reach a point one day where most of the code will be in Ruby :wink:

bw,
Peter

Excellent, then we are working toward similar ends: the invasion of the Java
world by Rubyists. Please feel free to ask questions on the JRuby mailing
lists at sf.net/projects/jruby and let us know how things are working out
for you. The compatibility issue is one of special concern for us...we'd
like to be able to run everything Ruby out of the box, and perhaps this is
not something Jython can claim today.

···

On 4/6/06, Peter Szinek <peter@rt.sk> wrote:

Howdy,

Charles O Nutter wrote:
> Forking the discussion a bit, but I'm interested in your use of JRuby
here.
> It seems like a great idea to have little snippits of code that are
easier
> to understand and maintain in Ruby, and be able to call them from Java
with
> JRuby. Are you doing this already, or is this a first possible attempt
at
> it?

Well, this will be my first attempt at JRuby - but this should not mean
anything, as i begun to work with Ruby 2 weeks ago :wink: Nevertheless i
can answer your question: I did this with Jython and i liked it. (I do
not really like to code in java, (i prefer dynamically typed languages
over statically typed ones) i am just doing it because i am working on a
big project which is written in java.) So as soon as i can (and it makes
sense) i am rushing to Jython/Jruby. (actually i like Ruby much more
after 2 weeks than Python after 2 years :wink: So i will go with JRuby in
the future. Actually JRuby is a clear winner in the compatibility issue
imho (against Jython, which is faaaar from being Python2.4 compatible)

I think it is absolutely cool to do everything in JRuby which makes
sense (i.e. to open a file, i will not go to JRuby - although after 5
years as a Java professional i have to still look up the doc on how to
open a file every time - not kidding here - so maybe this is not a bad
idea either :wink: but operations which require string manipulation,
regexps, lists, hashes, slicing, metaprogramming, can leverage blocks
etc just to name a few are defintely ideal to be "outsorced" to JRuby.
Another ideal case is when the input and the output are both something
simple (e.g. String -> String) - i.e. they are not depending heavily on
non-built-in java objects.

My only problem is, that my other colleagues (and the company in
general) is java only (although i am heavily sowing the seeds of Ruby
also here :wink: and thus they do not really like the idea that someday
they could possibly have to maintain Ruby code. So i am infiltrating our
codebase slowly (and hope to reach a point one day where most of the
code will be in Ruby :wink:

bw,
Peter

--
Charles Oliver Nutter @ headius.blogspot.com
JRuby Developer @ jruby.sourceforge.net
Application Architect @ www.ventera.com