porting java methods to ruby

Discussion in 'Ruby' started by Martin Durai, Nov 21, 2007.

  1. Martin Durai

    Martin Durai Guest

    could any one help me out to solve this.

    Following is a function java

    Actually the following function has a character array as a return type

    public char[] getTextCharacters(int [] holderForStartAndLength)
    {
    }

    Could any one help me to do the same in ruby


    Thank you in advance
    --
    Posted via http://www.ruby-forum.com/.
     
    Martin Durai, Nov 21, 2007
    #1
    1. Advertising

  2. Martin Durai

    Peter Szinek Guest

    Hi,

    > public char[] getTextCharacters(int [] holderForStartAndLength)
    > {
    > }
    >
    > Could any one help me to do the same in ruby


    Oh sure!

    def getTextCharacters(holder_for_start_and_length)
    end

    > Actually the following function has a character array as a return type


    It doesn't matter. Ruby is a dynamic language. Please check out these
    slides:

    http://onestepback.org/articles/10things/

    Cheers,
    Peter

    ___
    http://www.rubyrailways.com
    http://scrubyt.org
     
    Peter Szinek, Nov 21, 2007
    #2
    1. Advertising

  3. 2007/11/21, Peter Szinek <>:
    > Hi,
    >
    > > public char[] getTextCharacters(int [] holderForStartAndLength)
    > > {
    > > }


    This won't even compile because there is no "return" statement. Also,
    using an array to pass two values is at best sub optimal. And since
    you do not provide any details about the class at hand nobody can
    really help you.

    > > Could any one help me to do the same in ruby

    >
    > Oh sure!
    >
    > def getTextCharacters(holder_for_start_and_length)
    > end


    LOL

    Actually, we can make this even *more* rubyish:

    def get_text_characters holder_for_start_and_length
    end

    :)

    Cheers

    robert

    --
    use.inject do |as, often| as.you_can - without end
     
    Robert Klemme, Nov 21, 2007
    #3
  4. Martin Durai

    Peter Szinek Guest

    Robert Klemme wrote:
    > 2007/11/21, Peter Szinek <>:
    >> Hi,
    >>
    >>> public char[] getTextCharacters(int [] holderForStartAndLength)
    >>> {
    >>> }

    >
    > This won't even compile because there is no "return" statement. Also,
    > using an array to pass two values is at best sub optimal. And since
    > you do not provide any details about the class at hand nobody can
    > really help you.
    >
    >>> Could any one help me to do the same in ruby

    >> Oh sure!
    >>
    >> def getTextCharacters(holder_for_start_and_length)
    >> end

    >
    > LOL
    >
    > Actually, we can make this even *more* rubyish:
    >
    > def get_text_characters holder_for_start_and_length
    > end


    Ah, thanks Robert :) The most trivial tasks are the easiest to screw
    up, right?

    ___
    http://www.rubyrailways.com
    http://scrubyt.org
     
    Peter Szinek, Nov 21, 2007
    #4
  5. Martin Durai

    Ryan Davis Guest

    On Nov 21, 2007, at 02:00 , Martin Durai wrote:

    > Actually the following function has a character array as a return type
    >
    > public char[] getTextCharacters(int [] holderForStartAndLength)
    > {
    > }



    Having done far too much of this recently, my guess is it'll look
    something like:

    > def get_text_characters
    > result = []
    >
    > # get the characters and start from whatever...
    >
    > return result, start
    > end


    I ignored the holderForStartAndLength because the name hints that they
    are a stupid java hack for not having rich return values. In
    get_text_characters we don't bother with length because our array (or
    string... depends on how you actually want to use it) knows it's
    length and we return the start offset with it. You'd call it like:

    > chars, offset = get_text_characters
     
    Ryan Davis, Nov 21, 2007
    #5
  6. Martin Durai

    Martin Durai Guest

    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

    Robert Klemme wrote:
    > 2007/11/21, Peter Szinek <>:
    >> Hi,
    >>
    >> > public char[] getTextCharacters(int [] holderForStartAndLength)
    >> > {
    >> > }

    >
    > This won't even compile because there is no "return" statement. Also,
    > using an array to pass two values is at best sub optimal. And since
    > you do not provide any details about the class at hand nobody can
    > really help you.
    >
    >> > Could any one help me to do the same in ruby

    >>
    >> Oh sure!
    >>
    >> def getTextCharacters(holder_for_start_and_length)
    >> end

    >
    > LOL
    >
    > Actually, we can make this even *more* rubyish:
    >
    > def get_text_characters holder_for_start_and_length
    > end
    >
    > :)
    >
    > Cheers
    >
    > robert


    --
    Posted via http://www.ruby-forum.com/.
     
    Martin Durai, Nov 21, 2007
    #6
  7. Martin Durai

    Robert Dober Guest

    On Nov 21, 2007 11:23 AM, Ryan Davis <> wrote:
    >
    > On Nov 21, 2007, at 02:00 , Martin Durai wrote:
    >
    > > Actually the following function has a character array as a return type
    > >
    > > public char[] getTextCharacters(int [] holderForStartAndLength)

    I do not like this ---------------------------------^

    def get_text_chars start=0, length=1

    IOW you cannot port Java to Ruby, it will remain Java in disguise.

    Robert
    --
    what do I think about Ruby?
    http://ruby-smalltalk.blogspot.com/
     
    Robert Dober, Nov 21, 2007
    #7
  8. 2007/11/21, Martin Durai <>:
    > 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
     
    Robert Klemme, Nov 21, 2007
    #8
  9. Martin Durai

    Martin Durai Guest

    Thank you robert,


    my task is to port java classes and methods to ruby just like tha above
    one.

    could you help me with this porting



    >
    > 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


    --
    Posted via http://www.ruby-forum.com/.
     
    Martin Durai, Nov 21, 2007
    #9
  10. The simplest conversion is this:

    def getTextCharacters(holholderForStartAndLength)
    case @eventType
    when TEXT
    if @usePC
    holderForStartAndLength[0] = @pcStart
    holderForStartAndLength[1] = @pcEnd - @pcStart
    return @pc
    else
    holderForStartAndLength[0] = @posStart
    holderForStartAndLength[1] = @posEnd - @posStart
    return @buf
    end
    when START_TAG, END_TAG, CDSECT, COMMENT, ENTITY_REF,
    PROCESSING_INSTRUCTION, IGNORABLE_WHITESPACE, DOCDECL
    holderForStartAndLength[0] = @posStart
    holderForStartAndLength[1] = @posEnd - @posStart
    return @buf
    when START_DOCUMENT, END_DOCUMENT
    holderForStartAndLength[0] = holderForStartAndLength[1] = -1
    return nil
    else
    raise "unknown text eventType: " + @eventType
    end
    end

    Of course this is untested and assumes that the class that defines it
    will also set up the various class variables (the @something in the
    code) and the constants (the SOMETHING in the code). You should also
    consider creating your own exception class for the raise.

    Please also read "Pounding A Nail: Old Shoe or Glass Bottle?" by Alex
    Papadimoulis at
    http://weblogs.asp.net/alex_papadimoulis/archive/2005/05/25/408925.aspx

    Why the hell are you not using an existing ruby xml library, you know
    that your's will be the slowest xml parser on earth?

    This endeavour is made of fail.
     
    Peter Hickman, Nov 21, 2007
    #10
  11. On Nov 21, 2007 10:25 AM, Martin Durai <> wrote:
    > Hi peter,
    >
    > i have attached my java code which i have to port to ruby. could you
    > help me with this


    <code snipped>

    I would refactor that code before you consider porting it to anything. Theres
    a lot of external dependencies and a lot of unsafe constructs. That single
    function uses the following instance members:
    usePC
    eventType
    pcStart
    pcEnd
    posStart
    posEnd
    pc
    buf
    getText

    You are also not doing a lot of input value checking, and the function is very
    non-DRY. All of the return values are side effects of the method,
    defined elsewhere.
    The position holder (the function argument) should be a class.

    However its par for the course in Java really. However even in Java you would
    find that function very difficult to unit test without complex mocking.

    You would never write a Ruby function that behaved like that. You would break
    up the responsibilities of the function into individual pieces
    (functions), then take advantage
    of Ruby's better literal syntax and conditionals to clear up the messy
    conditionals.
    (The Ruby switch statement is a thing of beauty).
     
    Richard Conroy, Nov 21, 2007
    #11
  12. 2007/11/21, Martin Durai <>:
    > Thank you robert,
    >
    >
    > my task is to port java classes and methods to ruby just like tha above
    > one.
    >
    > could you help me with this porting


    Sorry, you'll have to do the typing yourself. If you insist on an
    exact copy then that's not really that difficult, there are arrays and
    Strings etc.

    robert

    --
    use.inject do |as, often| as.you_can - without end
     
    Robert Klemme, Nov 21, 2007
    #12
  13. Robert Klemme wrote:

    > Actually, we can make this even *more* rubyish:
    >
    > def get_text_characters holder_for_start_and_length
    > end


    Is it actually more rubyish to omit the parenthesis? I often find it
    much harder to read, specially in e-mails with a variable-spaced font.

    Best regards,

    Jari Williamsson
     
    Jari Williamsson, Nov 21, 2007
    #13
  14. Peter Hickman wrote:

    > The simplest conversion is this:
    > [...]
    > holderForStartAndLength[0] = @pcStart
    > holderForStartAndLength[1] = @pcEnd - @pcStart


    I would probably write something like:
    holder_for_start_and_length = [@pcStart, @pcEnd - @pcStart]

    Best regards,

    Jari Williamsson
     
    Jari Williamsson, Nov 21, 2007
    #14
  15. 2007/11/21, Jari Williamsson <>:
    > Robert Klemme wrote:
    >
    > > Actually, we can make this even *more* rubyish:
    > >
    > > def get_text_characters holder_for_start_and_length
    > > end

    >
    > Is it actually more rubyish to omit the parenthesis? I often find it
    > much harder to read, specially in e-mails with a variable-spaced font.


    I left that to optimize for someone else. ;-)

    Seriously: most of the time I use brackets in method definitions - not
    necessary usages.

    Kind regards

    robert

    --
    use.inject do |as, often| as.you_can - without end
     
    Robert Klemme, Nov 21, 2007
    #15
  16. Martin Durai

    Robert Dober Guest

    On Nov 21, 2007 2:44 PM, Jari Williamsson
    <> wrote:
    > Robert Klemme wrote:
    >
    > > Actually, we can make this even *more* rubyish:
    > >
    > > def get_text_characters holder_for_start_and_length
    > > end

    >
    > Is it actually more rubyish to omit the parenthesis? I often find it
    > much harder to read, specially in e-mails with a variable-spaced font.

    ok that is not the case normally however,
    Nevertheless most people still think it is terrible (e.g. David
    Black), I believe - and some others, Ara IIRC - believe it is much
    easier to read.

    Consider this, your brain has not yet used to missing parens, and it
    is actually looking for them, that already is some unnecessary work,
    right ;).
    I really would love to know if Smalltalkers feel the same when they
    see Ruby using minium parens style, Rick?

    I guess I would use parens if I were paid to write Ruby and somebody
    paying askes me nicely (like "or you will get fired").
    So somebody ready to pay??? ;)

    Cheers
    Robert

    >
    > Best regards,
    >
    > Jari Williamsson
    >
    >




    --
    what do I think about Ruby?
    http://ruby-smalltalk.blogspot.com/
     
    Robert Dober, Nov 21, 2007
    #16
  17. On 21.11.2007 17:13, Robert Dober wrote:
    > On Nov 21, 2007 2:44 PM, Jari Williamsson
    > <> wrote:
    >> Robert Klemme wrote:
    >>
    >>> Actually, we can make this even *more* rubyish:
    >>>
    >>> def get_text_characters holder_for_start_and_length
    >>> end

    >> Is it actually more rubyish to omit the parenthesis? I often find it
    >> much harder to read, specially in e-mails with a variable-spaced font.

    > ok that is not the case normally however,
    > Nevertheless most people still think it is terrible (e.g. David
    > Black), I believe - and some others, Ara IIRC - believe it is much
    > easier to read.
    >
    > Consider this, your brain has not yet used to missing parens, and it
    > is actually looking for them, that already is some unnecessary work,
    > right ;).
    > I really would love to know if Smalltalkers feel the same when they
    > see Ruby using minium parens style, Rick?
    >
    > I guess I would use parens if I were paid to write Ruby and somebody
    > paying askes me nicely (like "or you will get fired").
    > So somebody ready to pay??? ;)


    No, but I can ask you nicely. :)

    Seriously: it just occurs to me that the topic of using or not using
    parens seems to come up much less frequently than the topic of whether
    to use curly braces or "do end" with blocks. Maybe that's an indication
    that people faster adjust to the missing parens, dunno.

    Kind regards

    robert
     
    Robert Klemme, Nov 21, 2007
    #17
  18. Martin Durai

    Ryan Davis Guest

    On Nov 21, 2007, at 02:41 , Robert Dober wrote:

    >>> public char[] getTextCharacters(int [] holderForStartAndLength)

    > I do not like this ---------------------------------^
    >
    > def get_text_chars start=0, length=1
    >
    > IOW you cannot port Java to Ruby, it will remain Java in disguise.


    nonsense... you can port it just fine if you allow for design changes.
    that is why I didn't pass in start or length at all... they're RETURN
    values, not arguments.
     
    Ryan Davis, Nov 21, 2007
    #18
  19. Martin Durai

    Robert Dober Guest

    On Nov 21, 2007 9:32 PM, Ryan Davis <> wrote:
    >
    > On Nov 21, 2007, at 02:41 , Robert Dober wrote:
    >
    > >>> public char[] getTextCharacters(int [] holderForStartAndLength)

    > > I do not like this ---------------------------------^
    > >
    > > def get_text_chars start=0, length=1
    > >
    > > IOW you cannot port Java to Ruby, it will remain Java in disguise.

    >
    > nonsense... you can port it just fine if you allow for design changes.
    > that is why I didn't pass in start or length at all... they're RETURN
    > values, not arguments.

    Ryan we are not going to write Java code in Ruby only because it can
    be done, are we?
    Probably I was not clear, of course you can Java code in a way that
    the Ruby interpreter produces the same results, but does this make it
    Ruby code?

    R.
    >
    >




    --
    what do I think about Ruby?
    http://ruby-smalltalk.blogspot.com/
     
    Robert Dober, Nov 21, 2007
    #19
  20. Martin Durai

    MenTaLguY Guest

    On Thu, 22 Nov 2007 06:50:07 +0900, "Robert Dober" <> wrote:
    >> > IOW you cannot port Java to Ruby, it will remain Java in disguise.

    >>
    >> nonsense... you can port it just fine if you allow for design changes.
    >> that is why I didn't pass in start or length at all... they're RETURN
    >> values, not arguments.


    > Ryan we are not going to write Java code in Ruby only because it can
    > be done, are we?


    IMO, Ryan's example was farthest from writing Java in Ruby of all the
    examples given in this thread.

    -mental
     
    MenTaLguY, Nov 21, 2007
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mark Wilson
    Replies:
    2
    Views:
    153
    Robert Feldt
    Sep 25, 2003
  2. vasudevram
    Replies:
    0
    Views:
    235
    vasudevram
    Jul 25, 2006
  3. Replies:
    1
    Views:
    106
    Logan Capaldo
    Aug 8, 2006
  4. Jeff Davis

    ruby 1.9 porting problem

    Jeff Davis, Jan 1, 2008, in forum: Ruby
    Replies:
    2
    Views:
    91
    Jeff Davis
    Jan 1, 2008
  5. Kenneth McDonald
    Replies:
    5
    Views:
    324
    Kenneth McDonald
    Sep 26, 2008
Loading...

Share This Page