On Tuesday 28 September 2004 04:29 pm, stevetuckner wrote:
> I often use the ||= idiom in ruby for late initialization. But was
> recently looking for an idiom that would allow me to overwrite a
> variable if the right side is defined without repeating myself.
>
> I have used the following:
>
> var = val if val
>
> but I have to repeat val and that violates DRY. I thought maybe &&=
> would work, but did not
>
> var &&= val
>
> That only works if var and val are non-nil.
>
> Any ideas out there?

Well, callcc won't rollback vars. I tried:

  val = 10
  callcc {|c| (var=val) ? () : c.call }

Which might have worked in future Ruby, but alas no. But then what about 
simple transactions? Isn't there a lib for something like:

  transaction(:var) do
    (var = val) ? commit : rollback
  end

Just some wild guesses.

T.