Afficher un message
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
 
Page generated in 0,05484 seconds with 9 queries