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 > question about instance_eval, dynamic methods and blocks
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
question about instance_eval, dynamic methods and blocks

Réponse
 
LinkBack Outils de la discussion
Vieux 14/09/2007, 17h45   #1
Chris Hall
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut question about instance_eval, dynamic methods and blocks

Not sure if I am attacking this correctly or not, so if anyone has any
insight, please assist.

Let me setup my situation

class Task

attr_reader :name

def initialize(name, &block)
@name = name

instance_eval <<-EOL
def run(&block)
yield if block_given?
# i need the block passed to initialize to go here
# but i am not sure how do do it
end
end
end
end

t = Task.new('do_something') { puts "I want this block to be the guts of
'run' method in t instance" }

t.run { "running #{@name}" }


basically i want to dynamically create the run method whenever i create
an instance of a task, and the guts of the run method to be the block
passed with .new but i also want the ability to have run accept a block
as well.

any is appreciated.
--
Posted via http://www.ruby-forum.com/.

  Réponse avec citation
Vieux 14/09/2007, 18h03   #2
Stefano Crocco
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: question about instance_eval, dynamic methods and blocks

Alle venerd=C3=AC 14 settembre 2007, Chris Hall ha scritto:
> Not sure if I am attacking this correctly or not, so if anyone has any
> insight, please assist.
>
> Let me setup my situation
>
> class Task
>
> attr_reader :name
>
> def initialize(name, &block)
> @name =3D name
>
> instance_eval <<-EOL
> def run(&block)
> yield if block_given?
> # i need the block passed to initialize to go here
> # but i am not sure how do do it
> end
> end
> end
> end
>
> t =3D Task.new('do_something') { puts "I want this block to be the guts of
> 'run' method in t instance" }
>
> t.run { "running #{@name}" }
>
>
> basically i want to dynamically create the run method whenever i create
> an instance of a task, and the guts of the run method to be the block
> passed with .new but i also want the ability to have run accept a block
> as well.
>
> any is appreciated.


I'm not sure whether I understand your question correctly. Can't you store =
the=20
block passed to initialize in an instance variable and call it from run?

class Task

attr_reader :name

def initialize(name, &block)
@name =3D name
@block =3D block
end

def run
yield if block_given?
@block.call
end

end

If you need to create the run method dynamically for other reasons, I think=
=20
the simplest way to achieve what you want is still to store the block passe=
d=20
to initialize in an instance variable:

class Task
=20
attr_reader :name

def initialize name, &block
@name =3D name
@block =3D block
instance_eval do
def self.run
yield if block_given?
@block.call
end
end
end

end

I hope this s

Stefano

  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 20h16.


É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,07577 seconds with 10 queries