You need to specify the type of the delegate in order to implicitly convert a 
Ruby lambda to it. System.Delegate is not specific enough. You need to use some 
subclass of Delegate.

So you can e.g. change the type of the array:

load_assembly 'System.Core'
a = System::Array[System::Action.of(Object)].new(5)
a[0] = lambda { |x| puts x }

or use an explicit delegate construction like so:

load_assembly 'System.Core'
a = System::Array[System::Delegate].new(5)
a[0] = System::Action.of(Object).new { |x| puts x }

Tomas

From: ironruby-core-boun...@rubyforge.org 
[mailto:ironruby-core-boun...@rubyforge.org] On Behalf Of Martin Smith
Sent: Monday, August 03, 2009 4:46 PM
To: ironruby-core@rubyforge.org
Subject: Re: [Ironruby-core] Getting a delegate in IronRuby

Thanks Jim,

I'm more trying to understand the underlying type semantics with delegates.  
How would I do something like this:


>>> a = System::Array[System::Delegate].new(5)
=> [nil, nil, nil, nil, nil]
>>> a[0] = lambda {|x| puts x }
mscorlib:0:in `InternalSetValue': Object cannot be stored in an array of this 
type. (System::InvalidCastException)
        from mscorlib:0:in `SetValue'
        from mscorlib:0:in `System.Collections.IList.set_Item'
        from :0:in `[]='
        from :0

>>> l = lambda {|x| puts x }
=> #<Proc:0x0000084@(unknown):1>
>>> a[0] = &l
unknown: syntax error, unexpected AMPERSAND 
(Microsoft::Scripting::SyntaxErrorException)

Thanks,
Martin
On Mon, Aug 3, 2009 at 12:22 PM, Jim Deville 
<jdevi...@microsoft.com<mailto:jdevi...@microsoft.com>> wrote:

For and event, you can use a block:



l = lambda {|s,e| do.stuff.here }

foo.event_name.add &l

foo.event_name.remove &l



Thanks,



JD



...there is no try

http://blog.jredville.com



From: 
ironruby-core-boun...@rubyforge.org<mailto:ironruby-core-boun...@rubyforge.org> 
[mailto:ironruby-core-boun...@rubyforge.org<mailto:ironruby-core-boun...@rubyforge.org>]
 On Behalf Of Martin Smith
Sent: Monday, August 03, 2009 11:47 AM
To: ironruby-core@rubyforge.org<mailto:ironruby-core@rubyforge.org>
Subject: [Ironruby-core] Getting a delegate in IronRuby



Hello,

I was wondering how I can get a delegate in IronRuby.

One thing I was thinking about doing was trying to attach an event handler with 
an EventInfo and it requires a delegate.  It has a method called 
AddEventHandler(object, Delegate), and i wanted to attach an event handler.

But I can't find any way to get a delegate in ruby.  How would you guys do it?

I know i can also use:

object.send(event_name) { |*e| ...... }

In that case how would i remove that event handler?

Thanks in advance,
Martin

_______________________________________________
Ironruby-core mailing list
Ironruby-core@rubyforge.org<mailto:Ironruby-core@rubyforge.org>
http://rubyforge.org/mailman/listinfo/ironruby-core

_______________________________________________
Ironruby-core mailing list
Ironruby-core@rubyforge.org
http://rubyforge.org/mailman/listinfo/ironruby-core

Reply via email to