PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.ruby > porting java methods to ruby
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
porting java methods to ruby

Réponse
 
LinkBack Outils de la discussion
Vieux 21/11/2007, 10h00   #1
Martin Durai
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut porting java methods to ruby

could any one 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 me to do the same in ruby


Thank you in advance
--
Posted via http://www.ruby-forum.com/.

  Réponse avec citation
Vieux 21/11/2007, 10h06   #2
Peter Szinek
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: porting java methods to ruby

Hi,

> public char[] getTextCharacters(int [] holderForStartAndLength)
> {
> }
>
> Could any one 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

  Réponse avec citation
Vieux 21/11/2007, 10h17   #3
Robert Klemme
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: porting java methods to ruby

2007/11/21, Peter Szinek <peter@rubyrailways.com>:
> 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 you.

> > Could any one 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

  Réponse avec citation
Vieux 21/11/2007, 10h22   #4
Peter Szinek
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: porting java methods to ruby

Robert Klemme wrote:
> 2007/11/21, Peter Szinek <peter@rubyrailways.com>:
>> 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 you.
>
>>> Could any one 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



  Réponse avec citation
Vieux 21/11/2007, 10h23   #5
Ryan Davis
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: porting java methods to ruby


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





  Réponse avec citation
Vieux 21/11/2007, 10h25   #6
Martin Durai
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: porting java methods to ruby

Hi peter,

i have attached my java code which i have to port to ruby. could you
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 <peter@rubyrailways.com>:
>> 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 you.
>
>> > Could any one 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/.

  Réponse avec citation
Vieux 21/11/2007, 10h41   #7
Robert Dober
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: porting java methods to ruby

On Nov 21, 2007 11:23 AM, Ryan Davis <ryand-ruby@zenspider.com> 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/

  Réponse avec citation
Vieux 21/11/2007, 10h47   #8
Robert Klemme
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: porting java methods to ruby

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

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 07h25.


Édité par : vBulletin® version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,15063 seconds with 16 queries