Executing a proc passed as an argument?

Folks:

This shows what I want, and obviously how NOT to do it.
I’ve apparently got the callback method into @cb, but how
on earth do I execute it?

···

class Callback

def initialize (v, &m)
@cb = m #store a callback method
@v = v #store an argument to pass the method
end

def exe

@cb(@v) #execute the stored method (not this way!)

p “called exe, value = #{@v}” #stored the value, and
p “stored method = #{@cb}” #maybe the proc?
end
end

x = Callback.new(“a value”, &proc{|v| p v})
x.exe

Thanks,
Alan Walkington

class Callback

def initialize (v, &m)
@cb = m #store a callback method
@v = v #store an argument to pass the method
end

def exe

@cb(@v) #execute the stored method (not this way!)

@cb.call(@v) #invokes method stored in @cb with @v as argument

p “called exe, value = #{@v}” #stored the value, and
p “stored method = #{@cb}” #maybe the proc?
end
end

x = Callback.new(“a value”, &proc{|v| p v})

x = Callback.new(“a value”){|v| p v} #much prettier, imho

x.exe

Hope this helps.

-michael

Michael C. Libby x@ichimunki.com http://www.ichimunki.com/ http://www.ichimunki.com/public_key.txt
···

On Tuesday 29 October 2002 19:36, Alan (Ursus Major) wrote:

Thanks, Michael

Alan

“michael libby” x@ichimunki.com wrote in message
news:200210292004.31296.x@ichimunki.com

···

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Tuesday 29 October 2002 19:36, Alan (Ursus Major) wrote:

class Callback

def initialize (v, &m)
@cb = m #store a callback method
@v = v #store an argument to pass the method
end

def exe

@cb(@v) #execute the stored method (not this way!)

@cb.call(@v) #invokes method stored in @cb with @v as argument

p “called exe, value = #{@v}” #stored the value, and
p “stored method = #{@cb}” #maybe the proc?
end
end

x = Callback.new(“a value”, &proc{|v| p v})

x = Callback.new(“a value”){|v| p v} #much prettier, imho

x.exe

Hope this helps.

-michael

Michael C. Libby x@ichimunki.com http://www.ichimunki.com/ http://www.ichimunki.com/public_key.txt -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.0.7 (GNU/Linux)

iD8DBQE9vz4t4ClW9KMwqnMRAinlAJ4rCuI7h5CX0+R6pXuC9qYp43N28gCfeX3a
7Hi9yK/+7XvHtdRiuV7Rp9c=
=NuZX
-----END PGP SIGNATURE-----