What does ||= represent in this situation?

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.

John M. wrote:

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?

John M. wrote:

John M. wrote:

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

On Sun, Jan 17, 2010 at 11:31 AM, John M. [email protected]
wrote:

   n -= 1

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.

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

Josh C. wrote:

On Sun, Jan 17, 2010 at 11:31 AM, John M. [email protected]
wrote:

   n -= 1

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?

anler hp wrote:

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?

On Mon, Jan 18, 2010 at 2:27 PM, John M. [email protected] wrote:

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?

irb(main):002:0> defined? a
=> nil
irb(main):003:0> a ||= “hello”
=> “hello”
irb(main):004:0> a
=> “hello”
irb(main):005:0> defined? a
=> “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.

John M. wrote:

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.

On Jan 18, 2010, at 7:27 AM, John M. wrote:

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs