Archive

Archive for the ‘Programming’ Category

Helping Nokogiri. Take II

December 31st, 2009 Serabe 8 comments
4218563829 bf9f5519dd m Helping Nokogiri. Take II
Nokogiri and Pizza, what else can you ask for?

Ok. My fault. Now, let’s go get some work done.

First, if you haven’t done it, read the previous post about helping nokogiri and forgot about the script and memory leak. It seems that there are more important issues, so let’s fix them first. First run jruby test/test_jruby.rb from Nokogiri root. You’ll see a lot of errors (27 by now) and failures (14). Choose one, and get it green. After that, send me a pull request.

Ok. That sounds simple, but what if  the number of errors or failures raises? The rule I use is simple: keep the sum of both numbers going down and having a failure is better than having an error.

On the other hand, if you take a look at test/test_jruby.rb, you’ll see that not every test is in there. There is a reason for that. Even keeping the number of test low, you get a lot of errors/failures. If that annoys me with just 50 failures, imagine if I had a couple of hundreds errors. When all is right, I’ll add some more to keep the fun on.

Photo by Paul Johnston.

article clipper Helping Nokogiri. Take II
 
share save 171 16 Helping Nokogiri. Take II
Categories: Java, JRuby, Programming, Ruby Tags: ,

Do you wanna help us with pure-Java Nokogiri?

December 1st, 2009 Serabe No comments

First things first, if you wanna help, you’ll need to clone the git repo. Just:

git clone git://github.com/tenderlove/nokogiri.git
cd nokogiri
git checkout --track -b java origin/java

Install the dependencies. Just:

rake install:deps

Because it uses some native libraries, you’ll need to do that with MRI. Finally, you’ll need to generate some files, just run jruby -S rake java:spec. For having a hprof file, you’ll need to run this script with the following command:

jr -J-Xmx32m -J-XX:+HeapDumpOnOutOfMemoryError nokogiri_doc_frag.rb

-J-Xmx32m limits the heap space to 32 Mb, and the other options makes the JVM to write a hprof file when a OutOfMemoryError is thrown. After that, you can inspect that file with the profiler you can find in NetBeans.

In next post, I’ll comment where I think the problem is.

article clipper Do you wanna help us with pure Java Nokogiri?
 
share save 171 16 Do you wanna help us with pure Java Nokogiri?
Categories: General, Java, JRuby, Programming, Ruby Tags: , ,

Final Status Update (or How to get Nokogiri in JRuby without FFI)

August 26th, 2009 Serabe No comments

Hi, all,

sorry for the silence all these months, but I’ve been working hard on Nokogiri. First things first, it is not complete yet. Anyway, I’m gonna tell you how to build a gem and start working with it.

3848826571 e2da6c85ed m Final Status Update (or How to get Nokogiri in JRuby without FFI)
cc Final Status Update (or How to get Nokogiri in JRuby without FFI) photo credit: Matthew Byrne

Clone the repo and checkout the java branch.

git clone git://github.com/tenderlove/nokogiri.git
git checkout --track -b java origin/java

Next step: build the gem. As easy as jruby -S rake java:gem. Maybe, you need to install rexical and racc. jruby -S rake install:deps would do.

Finally, you have a gem in the pkg folder. Install it, and you’re done.

Updated: Take a look here if you wanna help.

article clipper Final Status Update (or How to get Nokogiri in JRuby without FFI)
 
share save 171 16 Final Status Update (or How to get Nokogiri in JRuby without FFI)
Categories: Java, JRuby, Programming, Ruby Tags: , , ,

Status Update: RMagick4J, Nokogiri, ruby2java and a possible MagickWand4J

June 6th, 2009 Serabe No comments

It’s been long time since last status update, but there are some things to tell, so here I am.

Thankfully, this year I’m a GSoC student again (and my mentor is Tom too). The main part of my project would be porting Nokogiri to JRuby, so I haven’t code for RMagick for a while now.

Let’s start with the status update then.

Nokogiri

I’ve been working on Nokogiri for a while. I forked Charles’ repo in Github, and I’ve implemented some cool features. For example, today I got my XML::Reader implementation to pass all tests in test_reader.rb. I hope I’ll be able to make a release this month (cross your fingers).

On the other hand, I got my first patch accepted in Nokogiri’s main repo.

RMagick4J

Not to much work done here, sorry. I haven’t code anything for a while now. Migrating from mercurial to git is already planned, but before that I would like to do a few commits more. Anyway, I’m quite happy with this project. Some people are using it and reporting bugs (in the end, those little things are all that matters). What else can I ask for?

Please, if you find a bug, report it here.

MagickWand

Tim Hunter (creator of RMagick) released MagickWand recently. I’ve been considering porting it to JRuby too. I have to take a deeper look at the C code, but, by now, I think it could be a good way to lead RMagick4J development. If finally I port it, I will split RMagick4J in two projects (Magick4J and RMagick4J). This way, MagickWand4J and RMagick4J would share the same java codebase, as MagickWand and RMagick share ImageMagick.

ruby2java

Take a look here. Awesome, isn’t it? And as soon as I have some time to work on it, siesta will be out too…

P.D. By the way, no more personal stuff in this blog. That stuff is now here, and only in Spanish (sorry about that).

article clipper Status Update: RMagick4J, Nokogiri, ruby2java and a possible MagickWand4J
 
share save 171 16 Status Update: RMagick4J, Nokogiri, ruby2java and a possible MagickWand4J

RMagick4J 0.3.7 Happy Birthmonth released.

March 23rd, 2009 Serabe No comments

RMagick4J 0.3.7 Happy Birthmonth has been released!

RMagick is a Ruby binding to ImageMagick and GraphicsMagick. RMagick4J implements ImageMagick functionality and the C portions of RMagick for use with JRuby.

Current stable version: 0.3.7
Project URL: http://kenai.com/projects/rmagick4j
Installation: gem install rmagick4j

New effects:

  • Charcoal
  • Edge
  • Implode
  • Negate
  • Normalize
  • Shade
  • Solarize
  • Wave

(I think that’s all that you need to use simple_captcha)

Major bugs resolved:

  • blur_image produced different images from RMagick.
  • Draw#push and Draw#pop didn’t work correctly in some cases.

Furthermore, RMagick4J does not depend on jhlabs library anymore (the gem has lost some weight!).

Thanks to:

  • Tim Hunter and the ImageMagick team (obviously).
  • JRuby community for its support.
  • Everyone that has reported a bug, ask for help in the #jruby channel or suggest a new library compatibility.

Please try out your applications with rmagick4j and help us provide feedback (even birthmonth cake!). It is our goal to make a fully-compatible implementation of RMagick4j in JRuby.

You have some image examples here: http://www.serabe.com/rmagick4j

This is a very special release, cause March is my birthmonth.

Happy Birthmonth to:

  • SuperTaz
  • everyone else whose birthmonth is march
  • and me!
article clipper RMagick4J 0.3.7 Happy Birthmonth released.
 
share save 171 16 RMagick4J 0.3.7 Happy Birthmonth released.

RMagick4J: status update

February 27th, 2009 Serabe No comments

In first place, my apologies if the lack of a new release or new feature is disturbing someone.

Currently, I’m working on new effects and I need a good convolve method for them to work. In the begining, I tried to replicate the convolve method, but finally I prefer to use the ConvolveOp class, expanding the image previously for replicating some ImageMagick functionality about surrounding pixels (Java has only two options while ImageMagick has a few more). And here is the problem. I cannot get the bunch of code working. Maybe, I’m overlooking something; maybe I’m not. If I cannot get it working in two days or so, I will ask (or even cry) for help.

End of report.

article clipper RMagick4J: status update
 
share save 171 16 RMagick4J: status update
Categories: JRuby, Programming, Ruby Tags: , ,

Google, you are not doing it right.

February 26th, 2009 Serabe No comments

Today, I woke up with a quite sad and disgusting new. Google doesn’t let ADPs (Android Developer Phones) see and download neither copy-protected nor paid apps. Why? Because Google protects the apps using a simple method: “Let’s put the apps in a folder the user cannot see”. But there is the problem, ADP’s users can see every folder, and can copy from and to them. I don’t know who the security guys are but avoiding developers to use that kind of apps is not the solution. If hiding the folder is the new super-safe state-of-the-art piracy-avoiding technique let me say that it sucks. This kind of decisions makes me wonder what’s the actual reason the apps cannot be installed in the sd card. Maybe because it is harder to hide a folder there?

article clipper Google, you are not doing it right.
 
share save 171 16 Google, you are not doing it right.

If you are (un)happy with Ruby 1.8.7

February 12th, 2009 Serabe No comments

There are two interesting topics in the Ruby Forum being discussed right now. Both opened by George Brown (the guy behind Prawn). They are:

I’ve read every single message because backwards compatibility in Ruby 1.8.7 is something that I do not fully understand. It all started with this comment in Jaime’s blog. There, Jaime wondered if Ubuntu did the right thing by updating Ruby to 1.8.7, even if that version breaks rails (in fact, it did). Well, in this case, my humble opinion was yes, Ubuntu did well by updating Ruby. But now, let’s consider other things.

So far, I’ve seen two kind of complains against Ruby 1.8.7:

  • Coding working in 1.8.6 that doesn’t work in 1.8.7. I’ve been talking James Coglan about it, and the one of the errors was that his code relied on the order of the keys in a hash. But, there are other he hasn’t been able to fix, and is something regarding regular expression. I am unhappy with this kind of “new features”.
  • Other people is complaining about working code in 1.8.7 that does not work in 1.8.6. I really understand them, because they program really cool gems and they have to test if they have used not-valid-1.8.6 code. I wouldn’t care too much about but we must keep in mind that 1.8.7 is a minor release.

Finally, I just want to say that, as many people has pointed out before me, it would be better to migrate to 1.9.1. Common! It has been already released! Anyway, that’s not the point of the discussion. It is all about a minor release with too many changes (I’m not talking about bugfixes). Do I like the new things? Yes, I do (except some that I cannot really understand). Am I happy with Ruby 1.8.7? No, I’m not because it is suppose to be a minor version release and it is making too much noise.

article clipper If you are (un)happy with Ruby 1.8.7
 
share save 171 16 If you are (un)happy with Ruby 1.8.7

Why I like JRuby

November 7th, 2008 Serabe No comments

Some days ago Tom forwarded me an email from Mikael Lammentausta. Mikael found an issue with rmagick4j. Using this Mikael's script

RUBY:
  1. require 'rubygems'
  2. require 'gruff'
  3.  
  4.   def basic_graph()
  5.     g = Gruff::Line.new
  6.     g.theme = {
  7.       :colors => ['#7F0099', '#2F85ED', '#2FED09','#EC962F'],
  8.       :marker_color => '#aaa',
  9.       :background_colors => ['#E8E8E8','#B9FD6C']
  10.     }
  11.     g.hide_title = true
  12.  
  13.     g.legend_font_size = 12
  14.     g.marker_font_size = 16
  15.     g.hide_dots = false
  16.  
  17.     return g
  18.   end
  19.  
  20.  
  21.   def bmi(params={})
  22.     g = basic_graph()
  23.  
  24.     g.y_axis_label = 'BMI'
  25.  
  26.     bmis = [24.3, 23.9, 23.7, 23.7, 23.6, 23.9, 23.6, 23.7, 23.4, 23.4, 23.4, 22.9]
  27.  
  28.     g.data( 'BMI', bmis )
  29.     g.hide_legend = true
  30.     return g
  31.   end
  32.  
  33.  
  34. g=bmi
  35. g.write 'graph.jpg'

You get two different outputs.

graph 300x225 Why I like JRuby

MRI-generated

graph1 300x225 Why I like JRuby

JRuby

As you may see, the numbers are wrong in JRuby... or not.

Looking at gruff code, I saw the problem was in the label method.

RUBY:
  1. def label(value)
  2.   if (@spread.to_f % @marker_count.to_f == 0) || !@y_axis_increment.nil?
  3.     return value.to_i.to_s
  4.   end
  5.   if @spread> 10.0
  6.     sprintf("%0i", value)
  7.   elsif @spread>= 3.0
  8.     sprintf("%0.2f", value)
  9.   else
  10.     value.to_s
  11.   end
  12. end

The problem can be found in line 10. With a right input, you can make

RUBY:
  1. (@spread.to_f % @marker_count.to_f == 0) || !@y_axis_increment.nil?

false and have a @spread < 3.0. Then look at this code:

RUBY:
  1. i = 23.599999999999998
  2. puts i

In JRuby, you get what you expect 23.599999999999998 but, in MRI, you get 23.6. So, that was the problem. JRuby is more precise than MRI.

NOTE:
jruby 1.1.5 (ruby 1.8.6 patchlevel 114) (2008-11-03 rev 7996) [i386-java]
ruby 1.8.6 (2008-03-03 patchlevel 114) [universal-darwin9.0]

article clipper Why I like JRuby
 
share save 171 16 Why I like JRuby
Categories: General, JRuby, Programming, Ruby Tags: , , ,

Problem 3

October 12th, 2008 Serabe No comments

Wording: (Original) The prime factors of 13195 are 5, 7, 13 and 29.
What is the largest prime factor of the number 600851475143 ?

Solution:
First the code:

RUBY:
  1. include Math
  2. def prime_factors(num, factor=2)
  3.   return []  if num <= 1
  4.   next_pf = (factor..(sqrt(num).ceil)).find(lambda {num}){ |x| num%x == 0 }
  5.   return [next_pf] + prime_factors(num/next_pf, next_pf)
  6. end
  7.  
  8. puts prime_factors(600851475143).max

The code is a direct port from the one in PyEuler. It is based mainly in the idea that the first factor of a number is always prime.

Line 3 breaks recursion and line 4 works out the next prime factor (actually, it finds the smallest factor). For doing this, it uses find method. If no element match the criteria, then it returns the number (that is what the lambda block is for).

Finally, line 5 merges the results and line 8 print the result.

This time just one code is showed.

article clipper Problem 3
 
share save 171 16 Problem 3
Improve the web with Nofollow Reciprocity.