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.
573b9499030e1ccb867ef80f0ff1ac49?d=identicon&s=25 Justin Bailey (Guest)
on 2006-03-29 00: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
81d609425e306219d54d793a0ad98bce?d=identicon&s=25 Matthew Moss (Guest)
on 2006-03-29 00: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
81d609425e306219d54d793a0ad98bce?d=identicon&s=25 Matthew Moss (Guest)
on 2006-03-29 00:41
(Received via mailing list)
Actually, I like using .first instead of [0] a tad better.
Cb48ca5059faf7409a5ab3745a964696?d=identicon&s=25 unknown (Guest)
on 2006-03-29 00:54
(Received via mailing list)
On Wed, 29 Mar 2006, Justin Bailey 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
57e939ff00f9df2670a09f417c08da5e?d=identicon&s=25 Josh Knowles (Guest)
on 2006-03-29 01:09
(Received via mailing list)
On 3/28/06, Matthew Moss <matthew.moss.coder@gmail.com> 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
E34b5cae57e0dd170114dba444e37852?d=identicon&s=25 Logan Capaldo (Guest)
on 2006-03-29 05:43
(Received via mailing list)
On Mar 28, 2006, at 5:53 PM, ara.t.howard@noaa.gov 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
31ab75f7ddda241830659630746cdd3a?d=identicon&s=25 Austin Ziegler (Guest)
on 2006-03-31 22:55
(Received via mailing list)
On 3/28/06, ara.t.howard@noaa.gov <ara.t.howard@noaa.gov> 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
Cb48ca5059faf7409a5ab3745a964696?d=identicon&s=25 unknown (Guest)
on 2006-03-31 23:50
(Received via mailing list)
On Sat, 1 Apr 2006, Austin Ziegler 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.