From: Erwin Abbott Date: 2007-07-10T13:52:58+09:00 Subject: Re: Syntax sugar: method[](...) => method(...) Hi Logan, Really cool stuff going on there, that was worth a thousand words. I never knew about this UnboundMethod#bind thing, curious if Proc has a similar mechanism. Also happy to learn Method#[] is an alias to Method#call Let me summarize what's going on here to make sure I understand. First we're creating an UnboundMethod from the given block. Next we create the named method, which returns a blank object with a [] method that calls the block. Okay, that was simpler than I thought! One thing I'll be trying to work out is calling the method "logins" will return a plain Object instead of a list of @events that are of the type :login. I think for my purposes defining a few more singleton methods like obj.each and obj.size would be good enough. On 7/9/07, Logan Capaldo wrote: > class Module > def bracket(name, &code) > define_method("#{name}_bracketed", &code) > m = instance_method("#{name}_bracketed") > remove_method "#{name}_bracketed" > > define_method(name) do || > obj = Object.new > m1 = m.bind(self) > (class << obj; self; end).module_eval { define_method(:[]) { |x| > m1[x] } } > obj > end > end > end > > > class Foo > bracket :logins do |date| > @data.select { |e| e.date == date } > end > end > > Who needs sugar? > > (I so didn't test this at all) Thanks, - Erwin