Thanks Ivan.  Actually, I didn't know this.  I'm kind of learning Ruby
generally as I embed it into my app.  I'm super familiar with .NET and the
CLR and all it's workings, but far less-so with ruby.  Thanks for the help

Martin

On Tue, Aug 4, 2009 at 4:43 PM, Ivan Porto Carrero <i...@flanders.co.nz>wrote:

> Out of curiosity why, for C# to consume?
>
> Ruby doesn't have an event syntax, it's an add-on for CLR interop.
> In Ruby you would probably want to use an observable implementation and you
> could, if you want to do so, add the add_event_handler and
> remove_event_handler methods.
> You probably know this already but ruby has a built-in Observable mixin
> that implements most of it for you. If you want to mimic .NET events you'll
> need a custom implementation that notifies the observers and includes the
> sender.
>
> module Observable
>
>     #
>     # Add +observer+ as an observer on this object. +observer+ will now 
> receive
>     # notifications. +observer+ is interested in the specified +event+
>     #
>     def add_observer(event, &observer)
>
>
>       @observers = [] unless defined? @observers
>       unless observer.respond_to? :call
>         raise NoMethodError, "observer needs to respond to 'update'"
>       end
>       @observers << { :event => event, :observer => observer }
>
>
>     end
>
>     #
>     # Delete +observer+ as an observer on this object. It will no longer 
> receive
>     # notifications of the specified +event+.
>     #
>     def delete_observer(event, &observer)
>       evt = { :event => event, :observer => observer }
>
>
>       @observers.delete evt if defined? @observers
>     end
>
>     #
>     # Delete all observers associated with this object.
>     #
>     def delete_observers
>       @observers.clear if defined? @observers
>
>
>     end
>
>     #
>     # Return the number of observers associated with this object.
>     #
>     def count_observers
>       if defined? @observers
>         @observers.size
>       else
>         0
>       end
>
>
>     end
>
>     #
>     # Notifies the registered observers that some interesting
>     # +event+ has occurred. It will notify the interested parties
>     # by calling the block and passing it some context
>
>     #
>
>     def notify_observers(event, sender, *args)
>       @observers.select {|evt| evt[:event] == event }.each {|evt| 
> evt[:observer].call sender, *args } unless count_observers.zero?
>     end
>
>
>   end
>
>
>
> ---
> Met vriendelijke groeten - Best regards - Salutations
> Ivan Porto Carrero
> Blog: http://flanders.co.nz
> Twitter: http://twitter.com/casualjim
> Author of IronRuby in Action (http://manning.com/carrero)
>
>
>
>
> On Wed, Aug 5, 2009 at 1:21 AM, Martin Smith <martin.smith...@gmail.com>wrote:
>
>> Thanks. Part of this question was also about returning and creating
>> RubyEvent objects.  Is there a way to declare an event on the ruby side
>> without a "RubyEvent" object?
>>
>> Thanks,
>> Martin
>>
>>
>>
>> On Tue, Aug 4, 2009 at 12:39 PM, Tomas Matousek <
>> tomas.matou...@microsoft.com> wrote:
>>
>>> BTW:
>>>
>>>
>>>
>>> Instead of using Reflection
>>>
>>>
>>>
>>> click_event = button.GetType.get_event("Click")
>>> on_click = EventHandler.new { |sender, e| puts "Click!" }
>>> click_event.add_event_handler(button, on_click)
>>>
>>>
>>>
>>> you can do
>>>
>>>
>>>
>>> handler = lambda { |sender, e| puts “Click!” }
>>>
>>> button.on_click.add handler
>>>
>>> button.on_click.remove handler
>>>
>>>
>>>
>>> or just
>>>
>>>
>>>
>>> button.on_click { |sender, e| puts “Click!” }
>>>
>>>
>>>
>>> if you don’t need to remove the handler.
>>>
>>>
>>>
>>> Tomas
>>>
>>>
>>>
>>>
>>>
>>> *From:* ironruby-core-boun...@rubyforge.org [mailto:
>>> ironruby-core-boun...@rubyforge.org] *On Behalf Of *Jimmy Schementi
>>> *Sent:* Tuesday, August 04, 2009 12:15 PM
>>>
>>> *To:* ironruby-core@rubyforge.org
>>> *Subject:* Re: [Ironruby-core] Getting a delegate in IronRuby
>>>
>>>
>>>
>>> I believe that commit is the one that didn't get into the binary build of
>>> 0.9.0, but is (strange enough) tagged with 0.9 in GitHub. Thanks for
>>> pointing that out though.
>>>
>>>
>>>
>>> *From:* ironruby-core-boun...@rubyforge.org [mailto:
>>> ironruby-core-boun...@rubyforge.org] *On Behalf Of *Martin Smith
>>> *Sent:* Tuesday, August 04, 2009 12:05 PM
>>> *To:* ironruby-core@rubyforge.org
>>> *Subject:* Re: [Ironruby-core] Getting a delegate in IronRuby
>>>
>>>
>>>
>>> Thank you all for your suggestions. They've been most helpful. I think
>>> i've mostly wrapped my head around event handlers.  Here's one last
>>> question.  What's the best way to test if a given object is an event?
>>> Obviously the way below: (object.GetType.get_event("EventName") != nil) is
>>> one way, but is there a more efficient (more rubonic) way?
>>>
>>> I've tried:
>>>
>>> button = Button.new
>>> button.click # => IronRuby.Builtins.RubyEvent
>>> # I cant test against IronRuby::Builtins::RubyEvent, because it's not
>>> defined
>>> button.click.is_a?(System::EventHandler)
>>> # error, about TypeGroups
>>> button.click.is_a?(System::EventHandler[]) # false
>>>
>>> As an aside, I looked at the changelog for, 0.9.0 and saw this:
>>>
>>>        2) Implements TypeGroup#[] overload taking a Fixnum. This is useful 
>>> when one
>>>
>>>                  needs to select a generic type definition out of a group 
>>> of types. For example,
>>>                        given three classes C:
>>>
>>>                       public class C {
>>>
>>>
>>>
>>>                         public virtual int Arity { get { return 0; } }
>>>                       }
>>>                       public class C<T> {
>>>
>>>                         public virtual int Arity { get { return 1; } }
>>>
>>>
>>>
>>>                       }
>>>                       public class C<T,S> {
>>>                         public virtual int Arity { get { return 2; } }
>>>                       }
>>>
>>>                       p C[0]  # => C
>>>
>>>
>>>
>>>                       p C[1]  # => C[T]
>>>                       p C[2]  # => C[T, S]
>>>
>>>
>>> However, this fails:
>>>
>>> System::EventHandler[0] # invalid value for class: 0
>>>
>>> You guys have been super helpful.  It's really great to see such an
>>> active and great community forming around IronRuby.
>>>
>>> Thanks,
>>> Martin
>>>
>>> On Mon, Aug 3, 2009 at 6:52 PM, Ray Vernagus <r.verna...@gmail.com>
>>> wrote:
>>>
>>> ** I tried sending this much earlier but my message didn't go through **
>>>
>>> Hi, Martin--
>>>
>>> Here's a short example that might illustrate what you're trying to
>>> accomplish:
>>>
>>> require "System.Windows.Forms"
>>> include System
>>> include System::Windows::Forms
>>>
>>> button = Button.new
>>>
>>> click_event = button.GetType.get_event("Click")
>>>
>>> on_click = EventHandler.new { |sender, e| puts "Click!" }
>>>
>>> click_event.add_event_handler(button, on_click)
>>>
>>> button.perform_click # Click!
>>>
>>> click_event.remove_event_handler(button, on_click)
>>>
>>> button.perform_click
>>>
>>> More canonical examples of using events in IronRuby are found here:
>>> http://www.ironruby.net/Documentation/.NET/Events
>>>
>>> --Ray
>>>
>>> On Mon, Aug 3, 2009 at 2:46 PM, Martin Smith <martin.smith...@gmail.com>
>>> wrote:
>>>
>>> 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
>>> http://rubyforge.org/mailman/listinfo/ironruby-core
>>>
>>>
>>>
>>> _______________________________________________
>>> Ironruby-core mailing list
>>> 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
>>>
>>>
>>
>> _______________________________________________
>> Ironruby-core mailing list
>> 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
>
>
_______________________________________________
Ironruby-core mailing list
Ironruby-core@rubyforge.org
http://rubyforge.org/mailman/listinfo/ironruby-core

Reply via email to