[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Opposite of ||= pattern?

jgbailey

3/28/2006 10:24:00 PM

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
7 Answers

matthew.moss.coder

3/28/2006 10:39:00 PM

0

You could make a class "Once" to wrap your value.
Or something cheesy like this:

def name
(x, @name = @name, nil)[0]
end


matthew.moss.coder

3/28/2006 10:41:00 PM

0

Actually, I like using .first instead of [0] a tad better.

On 3/28/06, Matthew Moss <matthew.moss.coder@gmail.com> wrote:
> You could make a class "Once" to wrap your value.
> Or something cheesy like this:
>
> def name
> (x, @name = @name, nil)[0]
> end
>


Ara.T.Howard

3/28/2006 10:54:00 PM

0

Josh Knowles

3/28/2006 11:09:00 PM

0

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

Logan Capaldo

3/29/2006 3:43:00 AM

0


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


Austin Ziegler

3/31/2006 8:54:00 PM

0

On 3/28/06, ara.t.howard@noaa.gov <ara.t.howard@noaa.gov> wrote:> On Wed, 29 Mar 2006, Justin Bailey wrote:> > 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.> harp:~ > ruby -e' 3.times{ p( @x ? nil : @x=42) } '> 42> nil> nilruby -e'first = true; 3.times { p first &&= nil }'The opposite of ||= is &&=; it may not be appropriate for therequested purpose, though. It allows for a "change only if set" sortof test. I think I've used it *once*.-austin--Austin Ziegler * halostatue@gmail.com * Alternate: austin@halostatue.ca

Ara.T.Howard

3/31/2006 9:47:00 PM

0