What does ||= represent in this situation?

J

John Merlino

Hey all, || is equal to Or. So what would ||= in this situation?

def expensive
@expensive ||=
begin
n = 99
buf = ""
begin
buf << "#{n} bottles of beer on the wall\n"
# ...
n -= 1
end while n > 0
buf << "no more bottles of beer"
end
end

Thanks for all suggestions. Also if you can interpret this, that would
be nice. What it appears here is an attempt of a do while loop.
 
J

John Merlino

John said:
Hey all, || is equal to Or. So what would ||= in this situation?

def expensive
@expensive ||=
begin
n = 99
buf = ""
begin
buf << "#{n} bottles of beer on the wall\n"
# ...
n -= 1
end while n > 0
buf << "no more bottles of beer"
end
end

Thanks for all suggestions. Also if you can interpret this, that would
be nice. What it appears here is an attempt of a do while loop.

I can't figure out how to edit a post. But I don't need anyone to
interpret the code from "begin" to "end". I know this is executing n
until it is no longer greater than 0. But what I'm trying to find is
something similar to what can be done in javascript:

In javascript, there are cases where you have a variable called, let's
say, old which contains 50 positions in an array. Now you want each
position of the array to be unique. But at the same time, you want to
generate random numbers for each item of the array:

var old = new array(20)
var new
do {
new = Math.floor(Math.random()*100) + 1;
}
while (old[new]);

old[new] = true;

Is there a way to do a do while loop like this in Ruby?
 
J

John Merlino

John said:
John said:
Hey all, || is equal to Or. So what would ||= in this situation?

def expensive
@expensive ||=
begin
n = 99
buf = ""
begin
buf << "#{n} bottles of beer on the wall\n"
# ...
n -= 1
end while n > 0
buf << "no more bottles of beer"
end
end

Thanks for all suggestions. Also if you can interpret this, that would
be nice. What it appears here is an attempt of a do while loop.

I can't figure out how to edit a post. But I don't need anyone to
interpret the code from "begin" to "end". I know this is executing n
until it is no longer greater than 0. But what I'm trying to find is
something similar to what can be done in javascript:

In javascript, there are cases where you have a variable called, let's
say, old which contains 50 positions in an array. Now you want each
position of the array to be unique. But at the same time, you want to
generate random numbers for each item of the array:

var old = new array(20)
var new
do {
new = Math.floor(Math.random()*100) + 1;
}
while (old[new]);

old[new] = true;

Is there a way to do a do while loop like this in Ruby?

Something like this?

def unique_num
@unique_num ||=
begin
old = [75] #The old variable holds 75 indexes of an array.
new = math.floor(math.random) * 100 + 1 #The new variable yields a
random number from 1 to 100.
begin
old << new #We append the random number into the array.
end while old[new] #We loop again if the number already exists, until
we get another number in the position.
old[new] = true #We set it to true since the number is now in the
array.
end
 
J

Josh Cheek

[Note: parts of this message were removed to make it a legal post.]

Hey all, || is equal to Or. So what would ||= in this situation?

def expensive
@expensive ||=
begin
n = 99
buf = ""
begin
buf << "#{n} bottles of beer on the wall\n"
# ...
n -= 1
end while n > 0
buf << "no more bottles of beer"
end
end

Thanks for all suggestions. Also if you can interpret this, that would
be nice. What it appears here is an attempt of a do while loop.
This code is (presumably) showing that it is expensive to calculate the
string "99 bottles of beer on the wall\n98 bottles ... \n no more bottles of
beer" and so what it does instead is calculates the string the first time
the method is called, but then not again after that.

The first time the method is called, @expensive will be nil, which evaluates
to false. So in the boolean expression, @expensive || "99 bottles ... " ,
the string will be returned. The ||= command can be thought of as
@expensive || ( @expensive = "99 bottles ... " )

In this case, if @expensive does not have a value, it will assign the string
to it. After that, however, it will have the value, and so the string will
not be assigned to it. This is useful, because the code that generates the
string is expensive to perform, and so we will never perform it unless we
know we need it, and after it has been performed, it should not have to be
performed again.
 
A

anler hp

hello, the operator | | =3D is the same as the typical + =3D, -=3D and =
so =20
on, why did exist in ruby and not in other languages is because in =20
ruby any expression returns a value.
here is an example very closed but pretty much less effective in other =20=

language

a =3D false;
a || a =3D c;

this means, makes a =3D c only if a is false=BA, same things happen with

a ||=3D c -- equivalent to --> a =3D a || c (if a is false, the right =20=

side of the || will be evaluated and its value will be returned -this =20=

only happen in ruby-, so c is returned either true or false)

sorry for my english
This code is (presumably) showing that it is expensive to calculate =20=
the
string "99 bottles of beer on the wall\n98 bottles ... \n no more =20
bottles of
beer" and so what it does instead is calculates the string the first =20=
 
J

John Merlino

anler said:
hello, the operator | | = is the same as the typical + =, -= and so
on, why did exist in ruby and not in other languages is because in
ruby any expression returns a value.
here is an example very closed but pretty much less effective in other
language

a = false;
a || a = c;

this means, makes a = c only if a is false�, same things happen with

a ||= c -- equivalent to --> a = a || c (if a is false, the right
side of the || will be evaluated and its value will be returned -this
only happen in ruby-, so c is returned either true or false)

sorry for my english

So if a is true, it is returned. If a is false, c is returned. What if a
is undefined?
 
J

John Merlino

Josh said:
This code is (presumably) showing that it is expensive to calculate the
string "99 bottles of beer on the wall\n98 bottles ... \n no more
bottles of
beer" and so what it does instead is calculates the string the first
time
the method is called, but then not again after that.

The first time the method is called, @expensive will be nil, which
evaluates
to false. So in the boolean expression, @expensive || "99 bottles ... "
,
the string will be returned. The ||= command can be thought of as
@expensive || ( @expensive = "99 bottles ... " )

In this case, if @expensive does not have a value, it will assign the
string
to it. After that, however, it will have the value, and so the string
will
not be assigned to it. This is useful, because the code that generates
the
string is expensive to perform, and so we will never perform it unless
we
know we need it, and after it has been performed, it should not have to
be
performed again.

From my understanding, you're saying since @expensive is undefined the
first time around, it is assigned the string "99 bottles". Since after
that first time, it is defined, it won't return the string again. Now my
question is if that's the case, what's the purpose of the while loop
here where n will loop as long as it's greater than 0?
 
A

Aldric Giacomoni

John said:
From my understanding, you're saying since @expensive is undefined the
first time around, it is assigned the string "99 bottles". Since after
that first time, it is defined, it won't return the string again. Now my
question is if that's the case, what's the purpose of the while loop
here where n will loop as long as it's greater than 0?

Here is how you really want to read this code:

def expensive
@expensive ||= {code block}
end

An assignment operator returns the assigned value. This means that
calling the method 'expensive' will return:
a) the code block, if @expensive is nil or undefined
b) @expensive otherwise

The purpose of the while loop is to set the value of @expensive. That's
it.
 
J

Jesús Gabriel y Galán

So if a is true, it is returned. If a is false, c is returned. What if a
is undefined?


irb(main):002:0> defined? a
=3D> nil
irb(main):003:0> a ||=3D "hello"
=3D> "hello"
irb(main):004:0> a
=3D> "hello"
irb(main):005:0> defined? a
=3D> "local-variable"

Think of it as an idiom to set a variable to a value only the first
time. The only caveat is to take care when false is a valid value for
such variable.

Jesus.
 
S

Stephen Celis

So if a is true, it is returned. If a is false, c is returned. What if a
is undefined?

The closest you'll get to an expansion of

a ||= c

is

defined?(a) && a || a = c

The variable will be assigned if it is nil, false, or undefined.

Stephen
 

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,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top