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 > Re: Using "sort!" in a C extension (1.9 problem)
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Re: Using "sort!" in a C extension (1.9 problem)

Réponse
 
LinkBack Outils de la discussion
Vieux 08/01/2008, 01h34   #1
Yukihiro Matsumoto
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Using "sort!" in a C extension (1.9 problem)

Hi,

Sorry for being late to reply. I have missed your mail.

In message "Re: Using "sort!" in a C extension (1.9 problem)"
on Sun, 30 Dec 2007 02:49:47 +0900, Andre Nathan <andre@digirati.com.br> writes:

|In ruby 1.8, I could use Array#sort! in a C extension like this:
|
|static VALUE
|call_sort_bang(VALUE ary)
|{
| return rb_funcall(ary, rb_intern("sort!"), 0);
|}
|
|static VALUE
|sort_i(VALUE ary)
|{
| long v1 = FIX2LONG(RARRAY_PTR(ary)[0]);
| long v2 = FIX2LONG(RARRAY_PTR(ary)[1]);
| return LONG2FIX(v1 - v2);
|}
|
|static VALUE
|foo(void)
|{
| VALUE ary = rb_ary_new();
| rb_ary_push(ary, INT2FIX(1));
| rb_ary_push(ary, INT2FIX(0));
| rb_ary_push(ary, INT2FIX(2));
| rb_iterate(call_sort_bang, ary, sort_i, 0);
| rb_funcall(rb_mKernel, rb_intern("p"), 1, ary);
| return a;
|}
|
|This would print "[0, 1, 2]" as expected. However, it doesn't work in
|ruby1.9 compiled from svn (checked out today). The problem seems to be
|that only one of the array elements is being passed to sort_i(), instead
|of a pair of elements as it's done in 1.8.
|
|What is the correct way to do this in 1.9?

In general, if you want to get multiple values from yield, you have to
use arguments added in 1.9:

static VALUE
sort_i(VALUE a1, VALUE a2, int argc, VALUE *argv)
{
long v1 = FIX2LONG(RARRAY_PTR(argv[0]));
long v2 = FIX2LONG(RARRAY_PTR(argv[1]));
return LONG2FIX(v1 - v2);
}

You may need to check argc if they are variable.

matz.

  Réponse avec citation
Vieux 08/01/2008, 02h34   #2
Andre Nathan
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Using "sort!" in a C extension (1.9 problem)

On Tue, 2008-01-08 at 09:34 +0900, Yukihiro Matsumoto wrote:
> In general, if you want to get multiple values from yield, you have to
> use arguments added in 1.9:
>
> static VALUE
> sort_i(VALUE a1, VALUE a2, int argc, VALUE *argv)
> {
> long v1 = FIX2LONG(RARRAY_PTR(argv[0]));
> long v2 = FIX2LONG(RARRAY_PTR(argv[1]));
> return LONG2FIX(v1 - v2);
> }
>
> You may need to check argc if they are variable.


Thanks Matz, Kubo ed me sorting this out.

Is there any situation where a1 will be different from argv[0] other
than the case Guy mentioned (argc == 0, and this argv[0] doesn't exist)?

Best,
Andre


  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 13h45.


Édité par : vBulletin®
Copyright ©2000 - 2009, 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 ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,42585 seconds with 10 queries