Archive

Posts Tagged ‘programming’

Transforming CSS selector to an XPath query

September 9th, 2011 Serabe No comments

So. Easy.

RUBY:
  1. require 'nokogiri'; Nokogiri::CSS.xpath_for(css_selector)

article clipper Transforming CSS selector to an XPath query
 
share save 171 16 Transforming CSS selector to an XPath query
Categories: Programming, Ruby Tags: , ,

The One where I spent a morning because -0.5 turned to be parsed as 1/2

September 5th, 2010 Serabe No comments

This summer, I have been doing some amazing stuff for GeoGebra. First of all, I would like to thank two people: Miguel, my mentor, who guided me through all that really advanced mathematical stuff; and Heinz Kredel, JAS creator and developer, who answered all my questions and implemented my suggestions as quickly as I could write them.

This story is about a problem we found in JAS. It was really subtle, so I may need a brief introduction to the project I worked on: LocusEqu. That project retrieves the equation of "any" geometrical locus. If you don't know what a locus is, Wolfram Math World defines it as "the set of all points (usually forming a curve or surface) satisfying some condition" . LocusEqu generates a few EquationExpression, and then, by using a EquationTranslator, it generates a GenPolynomial<BigRational> object.

For translating doubles, it calls the BigRational constructor whose only argument is a String. This is the code:

Can you spot the bug? Look at the 21st line. If s is "-0.5", then n is new BigInteger("-0"), and that's what makes "-0.5" be parsed as 1/2.

article clipper The One where I spent a morning because  0.5 turned to be parsed as 1/2
 
share save 171 16 The One where I spent a morning because  0.5 turned to be parsed as 1/2

Status update for GeoGebra.

July 12th, 2010 Serabe No comments

My contributions for the first part of the first part of GSoC 2010 can be divided in two: general improvements and coding.

General improvements

I made a few general improvements on GeoGebra:

  1. Refactored build.dir in ant build file. Previously, build.dir wasn't in the root directory.
  2. A few ant tasks were added, such compile-grammar, compile-oe (outside Eclipse), run-easyb and run-easyb-outside-eclipse.
  3. SVN properties were set in order to work outside Eclipse. This way, .class files will be kept out of the repo without the intervention of any Eclipse plugin.
  4. Easyb, a BDD groovy-based framework, has been included in order to test GeoGebra. It is not RSpec, but I guess it'll do.

Coding

First, I started creating a few EquationPoint classes,  currently there are six EquationPoint children classes:

Selección 003 Status update for GeoGebra.

  • EquationFreePoint represents an independent point.
  • EquationSymbolicPoint represents a dependent point,  EquationSpecialSymbolicPoint standing only for the locus point.
  • EquationNormalPoint and EquationPointVectorPoint are only auxiliar elements.

Then, a few EquationElement classes were added, these stand for the different constructions:

Selección 001 Status update for GeoGebra.

EquationElement is an abstract class containing a few basic methods:

  • forPoint: Given an EquationPoint, returns a String with the equation that means that the point is in the construction.
  • isAlgebraic: returns true if the construction is algebraic, and false otherwise.

Both EquationGenericCircle and EquationGenericLine are abstractions of specific line and circle contructions, all of them algebraic. EquationGenericSegment is to segment what EquationGenericLine is to lines. Obviously, EquationGenericSegment is not algebraic.

All of these classes are used together by EquationScope.

A pause for a screenshot.

Selección 002 modificada 300x132 Status update for GeoGebra.

Click for enlarge.

A glimpse into the future.

What to do next?:

  • Maybe Equation should be a proper class, not just a String.
  • More equations.
  • Working out the locus equation.
  • Not using an algorithm twice.
article clipper Status update for GeoGebra.
 
share save 171 16 Status update for GeoGebra.

Writing your own JRuby extension. Part II: Creating your first class.

March 12th, 2010 Serabe 1 comment

3783473433 98d837343a m Writing your own JRuby extension. Part II: Creating your first class.
What's the point of coding a JRuby extension if you don't create classes? Well, I cannot think of any case, but if you find one, please, let me know.

Do you remember Java's classes hierarchy? If so, you'll realize  that Java objects inherit from java.lang.Object by default but that is not actually what we need. But how can we tell JRuby that our object inherits from Object, the king of Ruby world? The answer is so simple: just extend RubyObject! (There is a RubyBasicObject too) Taken from Nokogiri::XML::Node:

Of course, you can extend any other class, as long as it is a "Ruby object". For example, Nokogiri::XML::Document extends from Nokogiri::XML::Node, and we do not need to do anything special to reflect it, just extend XmlNode like XmlDocument does:

After talking a bit about hierarchy, let's talk about Java constructors. At least two parameters are needed: a org.jruby.Ruby object and a org.jruby.RubyClass object. Being the importance of the former quite obvious, the reason for the latter may not be so clear. Let me show you some real world code: Nokogiri::XML::Node's dup method. Take a look at the following test:

Both new and dup methods in subclass rely on Nokogiri::XML::Node's. In here, you can see the this snippet of code:

The rb_obj_class method returns the class of an object, in this case, self. This way, the new node will be an instance of the same class as the original node. That's the reason a RubyClass is needed in the constructor, in order to know which is the actual class being instantiated.

By the way, do not forget to call super with the Ruby and RubyClass objects.

Next step: creating methods.

cc Writing your own JRuby extension. Part II: Creating your first class. photo credit: quinn.anya

article clipper Writing your own JRuby extension. Part II: Creating your first class.
 
share save 171 16 Writing your own JRuby extension. Part II: Creating your first class.

Writing your own JRuby extension. First problem.

January 11th, 2010 Serabe No comments

3981364314 d4b30cb739 m Writing your own JRuby extension. First problem.
Maybe, when requiring your just created extension, you get a LoadError. If it is the first time you require it, it is quite likely that you have not followed JRuby requiring conventions. If you want to know how require works, you can find the best documentation ever about it in the comment before org.jruby.runtime.load.LoadService class.

cc Writing your own JRuby extension. First problem. photo credit: cesarastudillo

article clipper Writing your own JRuby extension. First problem.
 
share save 171 16 Writing your own JRuby extension. First problem.

Writing your own JRuby extension. Part I: BasicLibraryService.

January 8th, 2010 Serabe 1 comment

Note: not code in this post, but you can see the code in Github. Follow the links!

Writing a JRuby extension is very easy, but there are almost not post out there about it. As far as I know, there is only one, Ola's. It is a really good tutorial indeed, but it lacks some details that might be not-that-easy to solve. Please, take some time to read it and, if some details are different, do follow Ola's way.

Everything's ready now, so let's start talking about BasicLibraryService. If you take a look at Nokogiri4J sourcecode, in ext/java/nokogiri folder, you will see a NokogiriService.java file. NokogiriService implements BasicLibraryService. This interface consists only of the method basicLoad which receives a Ruby object.

We will use this method to define classes and methods in the Ruby world. For defining a module, defineModule method is used with the name of the module. After that, modules and classes under that module can be defined easily by using the methods defineModuleUnder, which takes the name as parameter, and defineClassUnder, which takes the name and few parameters more. Let's dive into it.

defineClassUnder needs three arguments. The first one is the class' name. The second, is the parent class. If you have defined it previously, just passed it,  otherwise use RubyObject by calling the method getObject on the Ruby instance. The third parameter is an ObjectAllocator. ObjectAllocators returns intances of the classes in Java world. When instantiating Nokogiri::XML::Comment in Ruby world, JRuby will ask the ObjectAllocator for an instance of the Java class. It passes a Ruby object and the RubyClass being instantiated to the allocate method in the ObjectAllocator (more on RubyClass in following posts).

Finally, we will need to define some methods. Easiest way is by using the defineAnnotatedMethods. It takes a Java class as parameter. For knowing what this method does, you need to know a bit more about @JRubyMethod annotation (more on it in following post, have you realized the "Part I" in the title?). As you define methods, you may need to undefine some in a subclass. So easy! Use the undefineMethod method, which takes the name of the method as parameter (surprisingly, it undefines a method by redefining it!).

Next time, Implementing your first class.

article clipper Writing your own JRuby extension. Part I: BasicLibraryService.
 
share save 171 16 Writing your own JRuby extension. Part I: BasicLibraryService.

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

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

Programmers Jokes.

August 29th, 2008 Serabe No comments

In C we had to code our own bugs. In C++ we can inherit them.

C gives you enough rope to hang yourself. C++ also gives you the tree object to tie it to.

With C you can shoot yourself in the leg. With C++ you can reuse the bullet.

A computer without COBOL and Fortran is like a piece of chocolate cake without ketchup and mustard.

Why all Pascal programmers ask to live in Atlantis?
Because it is below C level.

Programmers Jokes.

article clipper Programmers Jokes.
 
share save 171 16 Programmers Jokes.

Hug a developer today.

August 28th, 2008 Serabe No comments

Hug a developer today. They are in pain. At least, I work for a Open Source project, so I have no boss... but me.

Anyway, you can hug me if you want icon smile Hug a developer today. .

Taken from.

article clipper Hug a developer today.
 
share save 171 16 Hug a developer today.
Improve the web with Nofollow Reciprocity.