2007/11/21, Martin Durai <martin / angleritech.com>:
> Hi peter,
>
> i have attached my java code which i have to port to ruby. could you
> help me with this
>
> public char[] getTextCharacters(int [] holderForStartAndLength)
>     {
>         if( eventType == TEXT ) {
>             if(usePC) {
>                 holderForStartAndLength[0] = pcStart;
>                 holderForStartAndLength[1] = pcEnd - pcStart;
>                 return pc;
>             } else {
>                 holderForStartAndLength[0] = posStart;
>                 holderForStartAndLength[1] = posEnd - posStart;
>                 return buf;
>
>             }
>         } else if( eventType == START_TAG
>                       || eventType == END_TAG
>                       || eventType == CDSECT
>                       || eventType == COMMENT
>                       || eventType == ENTITY_REF
>                       || eventType == PROCESSING_INSTRUCTION
>                       || eventType == IGNORABLE_WHITESPACE
>                       || eventType == DOCDECL)
>         {
>             holderForStartAndLength[0] = posStart;
>             holderForStartAndLength[1] = posEnd - posStart;
>             return buf;
>         } else if(eventType == START_DOCUMENT
>                       || eventType == END_DOCUMENT) {
>             //throw new XmlPullParserException("no content available to
> read");
>             holderForStartAndLength[0] = holderForStartAndLength[1] =
> -1;
>             return null;
>         } else {
>             throw new IllegalArgumentException("unknown text eventType:
> "+eventType);
>         }
>         //      String s = getText();
>         //      char[] cb = null;
>         //      if(s!= null) {
>         //          cb = s.toCharArray();
>         //          holderForStartAndLength[0] = 0;
>         //          holderForStartAndLength[1] = s.length();
>         //      } else {
>         //      }
>         //      return cb;
>     }
>
> All these code comes unde java version of pull parser

First of all I would create a class for the return values, like

TextSubRange = Struct.new :text, :start, :end

Then I would change all the if (x==..||x==...) to use a case statement.

Btw, I would do the same to the Java code (i.e. create another class
and use "switch").

Cheers

robert

-- 
use.inject do |as, often| as.you_can - without end