rubycollect4j – Ruby Collections for Java

As a Ruby programmer, I am fascinating by the friendly APIs which Ruby array & hash provide.

As a Java programmer, I am confident with the power and reliability it brings.

I tried JRuby and developed few Rails applications. Frankly speaking, it brought me a pretty good experience.

But when I am working on a pure Java project, JRuby doesn’t help much then.

In the end, I made my self a solution which is rubycollect4j, a pure Java library which implements all the methods refer to the Ruby array & hash.

 

For example:
It’s very easy to find 2 words which get the least and the most unique letters in a linux dictionary with Ruby.

File.foreach('/usr/share/dict/web2')
  .minmax_by { |word| word.chars.uniq.count }.map!(&:strip)
=> ["A", "blepharoconjunctivitis"]

Now you can do exactly the same thing in Java.

RubyFile.foreach("/usr/share/dict/web2")
  .minmaxBy(new TransformBlock<String, Integer>() {
    public Integer yield(String item) {
      return newRubyArray(item.split("(?!^)")).uniq().count();
    }
  });

It even looks better with Java 8:

RubyFile.foreach("/usr/share/dict/web2").minmaxBy((item) -> {
  return newRubyArray(item.split("(?!^)")).uniq().count();
});

 

The useful lazy loading feature in Ruby 2.0:

(1..100).lazy.cycle.first(10)

It’s fully available in Java now.

range(1, 100).lazy().cycle().first(10);

 

If now you have the same feeling as I do, visit my project home:

https://github.com/wnameless/rubycollect4j/

Advertisements
This entry was posted in java, ruby and tagged , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s