From: "Guy N. Hurst" Date: 2000-11-08T11:31:31+09:00 Subject: [ruby-talk:6150] Re: detect:ifNone: in Ruby Yukihiro Matsumoto wrote: > > Hi, > > In message "[ruby-talk:6111] Re: detect:ifNone: in Ruby" > on 00/11/07, Hugh Sasse Staff Elec Eng writes: > > |> How about default, defaults or defaulting (I like the last). > | > |I'd call it "nilGives" or "nil_gives", etc. I think default leaves too > |much open -- it depends on how your objects are made as to what their > |defualt value is. > > I agree with that `default' is too broad. And both `nilGives' (case > mixture; third person) and 'nil_gives (third person) are not Rubyish > neither. > Since this seems to be acting as a proxy, maybe a good name would be: proxy_nil And back to Hugh's 'cut' concept, maybe that can be named: trap_nil or nil_break myarray.sort!.nil_break.uniq!.nil_break.print (Alternatively, use proxy_nil{someObject} where someObject has method_missing defined accordingly ;-) Finally, I'd like to mention something that I have sometimes wished for - referring to previously mentioned variables or cached results. This is probably the ultimate in lazy programming, I don't know, but how many times have you wished you could stop repeating yourself in the following cases? if a == "up" || a== "down" || a == "around" then ..... Why not: if a == "up" ^or^ "down" ^or^ "around" ....... if month > 2 && month < 7 || month == 11 then ..... Why not: if month > 2 ^and^ < 7 ^or^ == 11 then ..... (abstract concept, not actually suggesting using ^ symbol) And how often have you made a calculation and needed to refer to again soon, only to realize you have to save it in a temporary variable to manually cache it? 1. if (co*m/ple+x%exp-ress/ion) then (co*m/ple+x%exp-ress/ion) else yield end temporary variable x = (co*m/ple+x%exp-ress/ion); if x then x else yield end can become something like if (co*m/ple+x%exp-ress/ion).cache_to(:x) then x else yield end existing alternative functional approach? if ( x = (co*m/ple+x%exp-ress/ion) ) then x else yield end existing proc approach? if (x = proc{(co*m/ple+x%exp-ress/ion)}).call then x.call else yield end 2. x = a.detect { |i| i == 7 } .nil? 99 : a.detect{ |i| i == 7 } temporary variable x = a.detect { |i| i == 7 }; if x.nil? then 99 else x end can become something like x = a.detect { |i| i == 7 }.cache_to(:tmp).nil? ? 99 : tmp existing alternative functional approach? x = ( tmp = a.detect { |i| i == 7 } ).nil? ? 99 : tmp proxy_nil could be used... x = a.detect { |i| i == 7 } .proxy_nil(99) Guy N. Hurst -- HurstLinks Web Development http://www.hurstlinks.com/ Norfolk, VA - (757)623-9688 PHP/MySQL - Ruby/Perl - HTML/Javascript