Scopro oggi che mentre ==, <=, < e compagnia sono dei metodi (questo lo sapevo), != invece non lo è. Ossia: $ irb > 1.send("==", 2) => false > 1.send("==", 1) => true >> 1.send("!=", 1) NoMethodError: undefined method `!=' for 1:Fixnum from (irb):3:in `send' from (irb):3 Cercando su http://www.ruby-doc.org/core/ del metodo != non c'è proprio traccia e neppure di !. Questo vuol dire che != viene trasformato dall'interprete nella negazione del risultato del metodo == ? Il tutto è nato da una tabellina di operatori da applicare a coppie di valori. Senza != mi tocca fare un caso speciale per trattarlo, ma non è un problema. Ho più che altro la curiosità di sapere come funziona != e perché non è implementato allo stesso modo degli altri metodi di comparazione. C'è qualche esperto che mi possa illuminare? Grazie! Paolo
on 2009-12-18 19:40
on 2009-12-20 09:43
Guardando il changelog, sembrano cose relativamente recenti:
Mon Feb 4 14:51:19 2008 Nobuyoshi Nakada <nobu@ruby-lang.org>
* parse.y (rb_enc_symname2_p): support "!", "!=" and "!~".
[ruby-dev:33592]
Mon Dec 10 01:35:06 2007 Yukihiro Matsumoto <matz@ruby-lang.org>
* parse.y (expr): redefinable not (!) operator.
* parse.y (arg): ditto.
* object.c (rb_obj_not): new method "!".
* object.c (rb_obj_not_equal): new method "!=".
* object.c (rb_obj_not_match): new method "!~".
Che versione di Ruby hai? Se scarichi i sorgenti, e fai cosi`:
find . -type f -print0 | xargs -0 -e fgrep -nH -e '"!="'
Cosa trovi?
Strano, comunque, sarei interessato a sapere come funzionava prima...
--
David N. Welton
http://www.welton.it/davidw/
http://www.dedasys.com/
Sent from Padua, Veneto, Italy
on 2009-12-20 11:33
Leggendo la tua risposta ho googlato ancora un po' con delle keyword diverse da quelle che avevo usato prima di postare la mia domanda ed ho trovato http://www.ruby-forum.com/topic/174382 != è diventato un metodo a partire dalla 1.9. Fino alla 1.8 l'interprete applicava il metodo == e ne negava il valore. Grazie Paolo David Welton wrote: > Guardando il changelog, sembrano cose relativamente recenti: > > Mon Feb 4 14:51:19 2008 Nobuyoshi Nakada <nobu@ruby-lang.org> > > * parse.y (rb_enc_symname2_p): support "!", "!=" and "!~". > [ruby-dev:33592] > > Mon Dec 10 01:35:06 2007 Yukihiro Matsumoto <matz@ruby-lang.org> > > * parse.y (expr): redefinable not (!) operator. > > * parse.y (arg): ditto. > > * object.c (rb_obj_not): new method "!". > > * object.c (rb_obj_not_equal): new method "!=". > > * object.c (rb_obj_not_match): new method "!~". > > Che versione di Ruby hai? Se scarichi i sorgenti, e fai cosi`: > > find . -type f -print0 | xargs -0 -e fgrep -nH -e '"!="' > > Cosa trovi? > > Strano, comunque, sarei interessato a sapere come funzionava prima... > > -- > David N. Welton > > http://www.welton.it/davidw/ > > http://www.dedasys.com/ > Sent from Padua, Veneto, Italy
Please log in before posting. Registration is free and takes only a minute.
Existing account
(Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
Log in with Google account | Log in with Yahoo account
No account? Register here.