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 > metaprogramming to generate list of class-specific attributes
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
metaprogramming to generate list of class-specific attributes

Réponse
 
LinkBack Outils de la discussion
Vieux 14/09/2007, 21h05   #1
Phlip
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut metaprogramming to generate list of class-specific attributes

Rubyists:

Here's a test case illustrates the problem. Then we'll have a candidate fix
and some questions.

class Kozmik
def self.acts_as_frog(species)
@@frog_list << species
end
@@frog_list = []
cattr_accessor :frog_list
end

class Toad < Kozmik
acts_as_frog :bufo
acts_as_frog :kermit
end

class BullFrog < Kozmik
acts_as_frog :tree
acts_as_frog :riparian
end

def test_fun_with_metaclasses
assert_equal [:tree, :riparian], BullFrog.frog_list
assert_equal [:bufo, :kermit], Toad.frog_list
end

Suppose we need an 'acts_as_' system to generate some methods for our target
classes. And suppose one method is a list of all the arguments passed to
each acts_as_() in each specific class.

The above test fails because @@ occupies the entire inheritance chain. So
both frog_list() class-methods return [:tree, :riparian, :bufo, :kermit].
The fix seems to be to reach into the meta-class for each derived class:

class Kozmik
def self.acts_as_frog(species)
@frog_list ||= []
@frog_list << species
self.class.send :attr_accessor, :frog_list
end
end

Is that as clean and lean as it could be? Is there some way to do this with
even fewer lines?

Next question: Couldn't class Kozmik be a module instead?

--
Phlip
http://www.oreilly.com/catalog/9780596510657/
^ assert_xpath
  Réponse avec citation
Vieux 14/09/2007, 22h21   #2
Joel VanderWerf
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: metaprogramming to generate list of class-specific attributes

Phlip wrote:
...
> class Kozmik
> def self.acts_as_frog(species)
> @frog_list ||= []
> @frog_list << species
> self.class.send :attr_accessor, :frog_list
> end
> end
>
> Is that as clean and lean as it could be? Is there some way to do this with
> even fewer lines?
>
> Next question: Couldn't class Kozmik be a module instead?


Sure...

module Kozmik
def acts_as_frog(species)
@frog_list ||= []
@frog_list << species
self.class.send :attr_accessor, :frog_list
end
end

class Toad; extend Kozmik
...
end

Or is that cheating somehow?

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

  Réponse avec citation
Vieux 14/09/2007, 22h27   #3
Phlip
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: metaprogramming to generate list of class-specific attributes

Joel VanderWerf wrote:

> class Toad; extend Kozmik


extend... different from include, right?

--
Phlip

  Réponse avec citation
Vieux 14/09/2007, 22h38   #4
Joel VanderWerf
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: metaprogramming to generate list of class-specific attributes

Phlip wrote:
> Joel VanderWerf wrote:
>
>> class Toad; extend Kozmik

>
> extend... different from include, right?


If you need it to be #include, you can always call #extend from within
#append_features as in ruby-talk:35979.

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

  Réponse avec citation
Vieux 14/09/2007, 22h45   #5
Ari Brown
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: metaprogramming to generate list of class-specific attributes


On Sep 14, 2007, at 5:00 PM, Phlip wrote:

> Rubyists:

Here is one answering
> class Kozmik
> def self.acts_as_frog(species)
> @frog_list ||= []
> @frog_list << species
> self.class.send :attr_accessor, :frog_list
> end
> end
>
> Is that as clean and lean as it could be? Is there some way to do
> this with
> even fewer lines?


I am not really sure if this works, but couldn't you do:

class Kozmik
def self.inherited
module_eval do
@frog_list = []
end
end

def self.acts_as_frog(species)
@frog_list << species
end
end

Also, does module_eval only work in a class method? Why can't I do
def something; module_eval do puts 5 end; end ?

Ari
-------------------------------------------|
Nietzsche is my copilot



  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 22h43.


É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,09661 seconds with 13 queries