Looking through the rails core code, I see method_missing performed like this a lot: def method_missing(method, *args) if block_given? current_status.send(method, *args) { |*block_args| yield(*block_args) } else current_status.send(method, *args) end end Why not just write: def method_missing(method, *args, &block) current_status.send(method, *args, &block) end It the above implementation much faster or do they perform differently? -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Sep 26, 11:53 pm, Aryk Grosz <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Looking through the rails core code, I see method_missing performed like > this a lot: > > def method_missing(method, *args) > if block_given? > current_status.send(method, *args) { |*block_args| > yield(*block_args) } > else > current_status.send(method, *args) > end > end > > Why not just write: > > def method_missing(method, *args, &block) > current_status.send(method, *args, &block) > end >Building the proc object (as implied by the use of &block) is more expensive than just passing a block: require ''benchmark'' include Benchmark def dummy() yield end def dummy_proc(&block) block.call end bm do |x| x.report(''with yield''){ 100000.times { dummy {y=1}}} x.report(''with &block'') { 100000.times { dummy_proc {y=1}}} end outputs (on a fairly elderly machine): user system total real with yield 0.420000 0.000000 0.420000 ( 0.464097) with &block 2.340000 0.040000 2.380000 ( 2.584321) Fred> It the above implementation much faster or do they perform differently? > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Fred, thanks for that, I still can''t believe that block.call is slower than yield. All this time, I thought I was speeding thing up by passing in the &block. thanks a bunch! -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Sent from my iPhone On 30 Sep 2008, at 00:07, Aryk Grosz <rails-mailing-list@andreas- s.net> wrote:> > Fred, thanks for that, I still can''t believe that block.call is slower > than yield. >If my memory is correct, it''s not the block call (try removing the yield/block.call from the benchmark) that is slow, but the coercing of the block into a proc Fred> All this time, I thought I was speeding thing up by passing in the > &block. > > thanks a bunch! > -- > Posted via http://www.ruby-forum.com/. > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---