Reuse passed proc later?

Hello Everyone,

I'm new to Ruby and some of its features...

I want to do the following (with as little modfication as necessary):

call a passed block to a function at some later time... Eg.

class A

  def foo(&block)
    @func = block
  end
end

a = A.new
num = a.func.call { print "12" }

...but func is an unknown method, and attr_accessor :func doesn't help either....

any nice solution?

David G.

···

--
_______________________________________________
Find what you are looking for with the Lycos Yellow Pages
http://r.lycos.com/r/yp_emailfooter/http://yellowpages.lycos.com/default.asp?SRC=lycos10

In your example you never call foo, so @func never gets assigned to.
Is this something like what you want to do?

  class A

    def func(&block)
      @func ||= block
    end
  end

  a = A.new
  a.func { 12 }

  num = a.func.call
  puts num # 12

David

···

On Sat, 30 Oct 2004, David Gurba wrote:

Hello Everyone,

I'm new to Ruby and some of its features...

I want to do the following (with as little modfication as necessary):

call a passed block to a function at some later time... Eg.

class A

  def foo(&block)
    @func = block
  end
end

a = A.new
num = a.func.call { print "12" }

...but func is an unknown method, and attr_accessor :func doesn't help either....

any nice solution?

--
David A. Black
dblack@wobblini.net

"David Gurba" <blue_technx@lycos.com> schrieb im Newsbeitrag news:20041030005556.A499DCA06F@ws7-4.us4.outblaze.com...

Hello Everyone,

I'm new to Ruby and some of its features...

I want to do the following (with as little modfication as necessary):

call a passed block to a function at some later time... Eg.

class A

def foo(&block)
   @func = block
end
end

a = A.new
num = a.func.call { print "12" }

..but func is an unknown method, and attr_accessor :func doesn't help either....

As David said, you don't invoke foo so you don't assign to @func. You got the order a bit wrong since you provide the block too late, i.e. when you want to call it. The smallest change to your example might be this

class A
  def foo(&block)
    @func = block
  end
end

a = A.new
num = a.foo { print "12" }.call

But you probably wanted

class A
  attr_reader :func
  def foo(&b) @func = b end
end

a = A.new
a.foo { puts "blah!" }
a.func.call

Kind regards

    robet

Hi,

David Gurba wrote:

I want to do the following (with as little modfication as necessary):

call a passed block to a function at some later time... Eg.

class A

  def foo(&block)
    @func = block
  end
end

a = A.new
num = a.func.call { print "12" }

..but func is an unknown method, and attr_accessor :func doesn't help either....

any nice solution?

Did you mean to do this:

class A

   attr_accessor :func

   def foo(&block)
     @func = block
   end

end

a = A.new
a.foo { print "12" }
a.func.call

__END__

HTH,
-- shanko