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: Fixnums can have instance variables? Cool.
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Re: Fixnums can have instance variables? Cool.

Réponse
 
LinkBack Outils de la discussion
Vieux 05/01/2008, 15h07   #1
Barca Junior
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Fixnums can have instance variables? Cool.


>
> class Fixnum
> attr_accessor :note
> end
>
> 13.note = "a baker's dozen"
> 25.note = "5 squared"
>
> notes = [13, 21, 25].collect { |x| x.note } # ["a baker's dozen",
> nil, "5 squared"]
>



It's really cool.
--
Posted via http://www.ruby-forum.com/.

  Réponse avec citation
Vieux 05/01/2008, 15h15   #2
Jason Roelofs
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Fixnums can have instance variables? Cool.

[Note: parts of this message were removed to make it a legal post.]

Welcome to Monkey Patching! This is how ActiveResource gives you things
like:

4.days.ago

"some string".underscore

and the like.

It's one of the many things that makes Ruby code very fun to write.

Jason

On Jan 5, 2008 10:07 AM, Barca Junior <wuzhengchun@gmail.com> wrote:

>
> >
> > class Fixnum
> > attr_accessor :note
> > end
> >
> > 13.note = "a baker's dozen"
> > 25.note = "5 squared"
> >
> > notes = [13, 21, 25].collect { |x| x.note } # ["a baker's dozen",
> > nil, "5 squared"]
> >

>
>
> It's really cool.
> --
> Posted via http://www.ruby-forum.com/.
>
>


  Réponse avec citation
Vieux 05/01/2008, 15h32   #3
Gary Wright
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Fixnums can have instance variables? Cool.


On Jan 5, 2008, at 10:15 AM, Jason Roelofs wrote:

> Welcome to Monkey Patching! This is how ActiveResource gives you
> things
> like:
>
> 4.days.ago
>
> "some string".underscore
>
> and the like.
>
> It's one of the many things that makes Ruby code very fun to write


Actually it is a bit different. Monkey patching is just adding
methods to classes. Fixnum and String in your examples.

Instance variables on Fixnums (or Symbols or Nil) is different.
It is a good example of uniformity in Ruby but I'm hard pressed
to think of a nice use case for the feature.

Gary Wright

  Réponse avec citation
Vieux 05/01/2008, 20h54   #4
Sander Land
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Fixnums can have instance variables? Cool.

On Jan 5, 2008 4:32 PM, Gary Wright <gwtmp01@mac.com> wrote:
> Actually it is a bit different. Monkey patching is just adding
> methods to classes. Fixnum and String in your examples.
>
> Instance variables on Fixnums (or Symbols or Nil) is different.
> It is a good example of uniformity in Ruby but I'm hard pressed
> to think of a nice use case for the feature.
>
> Gary Wright

You can use it to cache the result of method calls.

class Fixnum
def factorial
@factorial ||= self * (self-1).factorial
end
end
0.instance_variable_set('@factorial',1)

  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 11h53.


É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,11513 seconds with 12 queries