Forum: Ruby Opposite of ||= pattern?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Justin B. (Guest)
on 2006-03-29 02:24
(Received via mailing list)
A cheap just-in-time initialization trick is the "||=" trick:

def add_name(n)
  @name ||= Array.new
  @name << n
end

Now, how would you do the opposite of this pattern? More specifically,
what
kind of construct would evaluate to a true value once, and then nil from
then on? I came upon this in the context of for loops, where I want a
one-time "starting" value to be present the first time through the loop,
then nil. And I wanted to do it in a cool way  - i.e. not just assign
the
value to nil at the end of the loop, though of course that is the
easiest
way.

Any thoughts?

Justin
Matthew M. (Guest)
on 2006-03-29 02:39
(Received via mailing list)
You could make a class "Once" to wrap your value.
Or something cheesy like this:

def name
   (x, @name = @name, nil)[0]
end
Matthew M. (Guest)
on 2006-03-29 02:41
(Received via mailing list)
Actually, I like using .first instead of [0] a tad better.
unknown (Guest)
on 2006-03-29 02:54
(Received via mailing list)
On Wed, 29 Mar 2006, Justin B. wrote:

> one-time "starting" value to be present the first time through the loop,
> then nil. And I wanted to do it in a cool way  - i.e. not just assign the
> value to nil at the end of the loop, though of course that is the easiest
> way.
>
> Any thoughts?
>
> Justin

harp:~ > ruby -e' 3.times{ p( @x ? nil : @x=42) } '
42
nil
nil


regards.

-a
Josh K. (Guest)
on 2006-03-29 03:09
(Received via mailing list)
On 3/28/06, Matthew M. <removed_email_address@domain.invalid> wrote:
>
> You could make a class "Once" to wrap your value.



 class Object
   def once
     @used ? nil : (@used = true; self)
   end
 end

test = true

3.times { p test.once }
=> true
=> nil
=> nil
Logan C. (Guest)
on 2006-03-29 07:43
(Received via mailing list)
On Mar 28, 2006, at 5:53 PM, removed_email_address@domain.invalid wrote:

> harp:~ > ruby -e' 3.times{ p( @x ? nil : @x=42) } '
> 42
> nil
> nil

As an aside I was playing with ara's code (tried to put it in a
method) and I got a toggle variable:
irb(main):006:0> def once
irb(main):007:1>   @x = @x ? nil : 1
irb(main):008:1> end
irb(main):009:0> once
=> 1
irb(main):010:0> once
=> nil
irb(main):011:0> once
=> 1
irb(main):012:0> once
=> nil
irb(main):013:0> once
=> 1
Austin Z. (Guest)
on 2006-04-01 00:55
(Received via mailing list)
On 3/28/06, removed_email_address@domain.invalid 
<removed_email_address@domain.invalid> wrote:
> > then on? I came upon this in the context of for loops, where I want a
> > one-time "starting" value to be present the first time through the loop,
> > then nil. And I wanted to do it in a cool way  - i.e. not just assign the
> > value to nil at the end of the loop, though of course that is the easiest
> > way.

> harp:~ > ruby -e' 3.times{ p( @x ? nil : @x=42) } '
> 42
> nil
> nil

ruby -e'first = true; 3.times { p first &&= nil }'

The opposite of ||= is &&=; it may not be appropriate for the
requested purpose, though. It allows for a "change only if set" sort
of test. I think I've used it *once*.

-austin
unknown (Guest)
on 2006-04-01 01:50
(Received via mailing list)
On Sat, 1 Apr 2006, Austin Z. wrote:

>> harp:~ > ruby -e' 3.times{ p( @x ? nil : @x=42) } '
>> 42
>> nil
>> nil
>
> ruby -e'first = true; 3.times { p first &&= nil }'
>
> The opposite of ||= is &&=; it may not be appropriate for the
> requested purpose, though. It allows for a "change only if set" sort
> of test. I think I've used it *once*.

well heck - just used it for the first time today!

i guess the OP didn't actually want the literal opposite though...

regards.

-a
This topic is locked and can not be replied to.