Opposite of ||= pattern?

J

Justin Bailey

------=_Part_10670_29537698.1143584632451
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

A cheap just-in-time initialization trick is the "||=3D" trick:

def add_name(n)
@name ||=3D 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

------=_Part_10670_29537698.1143584632451--
 
M

Matthew Moss

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

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

ara.t.howard

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

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


regards.

-a
 
J

Josh Knowles

------=_Part_7317_15354438.1143587339424
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

You could make a class "Once" to wrap your value.



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

test =3D true

3.times { p test.once }
=3D> true
=3D> nil
=3D> nil

------=_Part_7317_15354438.1143587339424--
 
L

Logan Capaldo

--Apple-Mail-9--59641472
Content-Transfer-Encoding: 7bit
Content-Type: text/plain;
charset=US-ASCII;
delsp=yes;
format=flowed


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



--Apple-Mail-9--59641472--
 
A

ara.t.howard

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,772
Messages
2,569,593
Members
45,111
Latest member
KetoBurn
Top