From: "Ara.T.Howard" Date: 2004-08-06T01:31:28+09:00 Subject: Re: Macros in Ruby On Fri, 6 Aug 2004, Gavin Sinclair wrote: > On Friday, August 6, 2004, 12:11:27 AM, Ara.T.Howard wrote: > >> perhaps a shorthand: > >> lambda{ puts 'lambda' } => Proc:0xb74041a0 >> &{ puts 'lambda too' } => Proc:0xb74041a1 > >> i'm probably missing something, but i don't think the syntax '&{' occurs >> anywhere now and '&' is already synomynous with blocks/procs/etc. > > > If you must have a shortcut, I think 'L' makes more sense. > > L { puts 'lambda' } => Proc:0xb74041a0 but 'L' is a valid identifier: ~ > cat a.rb L = 42 L { puts 42 } ~ > ruby a.rb a.rb:2: undefined method `L' for main:Object (NoMethodError) from a.rb:2 problematic. > My aesthetic sense says that a shortcut is unnecessary because > creating explicit lambdas is uncommon (because Ruby has blocks, thank > goodness), and because when you do explicitly create lambdas, they're > usually so cool and saving so much other effort that I don't begrudge > them a few characters :) > > Gavin i actually use them quite a bit and would appreciate a shortcut. i sometimes prefer to have modifiable Proc attributes of object to parameterize their behaviour rather than messing with inheritence or dynamic redefinition. i also end up writing methods which take options callbacks for debugging/logging, eg: def longrunning_database_update arg, opts = {} pre, pan, post = opts.values_at 'pre', 'pan', 'post' sql = gen_sql arg pre.call sql if pre res = if pan t = Thread.new(Thread.current) do |c| begin execute sql rescue => e c.raise e end end loop do break unless t.status pan.call(get_state) sleep 0.42 end t.join t.value else execute sql end post.call res if post res end now the call looks somewhat like this longrunning_database_update 42, 'pre' => lambda{|sql| debug{ "sql <#{ sql }>" }}, 'pan' => lambda{|state| debug{ "state <#{ state }>" }}, 'post' => lambda{|res| debug{ "res <#{ res }>" }} i would like say longrunning_database_update 42, 'pre' => &{|sql| debug{ "sql <#{ sql }>" }}, 'pan' => &{|state| debug{ "state <#{ state }>" }}, 'post' => &{|res| debug{ "res <#{ res }>" }} of coures this is totally minor - it would be nice IMHO. -a -- =============================================================================== | EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov | PHONE :: 303.497.6469 | A flower falls, even though we love it; | and a weed grows, even though we do not love it. | --Dogen ===============================================================================