The former returns "some string", but the latter returns "a string". Apply that to the form you described and it basically means that if a[:i] is nil then it assigns "a string", otherwise it leaves it alone.
- Jake McArthur
路路路
On Jul 27, 2006, at 3:10 AM, rejoc wrote:
Hello,
I new to Ruby and curious about this language I do use (yeet).
I was browsing through some libraries/modules and I found a line looking like
a[:i] ||= "a string"
I could not find this syntax in any manual online (and googling "||=" is not of much help )
will return anotherObject (assuming it's value isn't nil)
The expression
a[:i] ||= "a string"
is equivalent to
a[:i] = a[:i] || "a string"
(sort of like 'num+= 1' is the same as 'num = num + 1')If a[:i] is not
nil then it will remain unchanged. If it is nil then it will be set to
"a string".
Farrel
路路路
On 27/07/06, rejoc <rejoc@freefree.fr> wrote:
Hello,
I new to Ruby and curious about this language I do use (yeet).
I was browsing through some libraries/modules and I found a line looking
like
a[:i] ||= "a string"
I could not find this syntax in any manual online (and googling "||=" is
not of much help )
it will return the value for a[:i] if a value exists, if not, it will set a[:i] to the value "a string".
路路路
On 27-Jul-06, at 4:10 AM, rejoc wrote:
Hello,
I new to Ruby and curious about this language I do use (yeet).
I was browsing through some libraries/modules and I found a line looking like
a[:i] ||= "a string"
I could not find this syntax in any manual online (and googling "||=" is not of much help )
Can anyone tell me what it means
--
Jeremy Tregunna
jtregunna@blurgle.ca
"One serious obstacle to the adoption of good programming languages is the notion that everything has to be sacrificed for speed. In computer languages as in life, speed kills." -- Mike Vanier
which means if var is defined and evaluates to true it is left alone
however if var is not defined or evaluates to false it is assigned expr.
That means also that
var ||= expr
will overwrite nil *and* false values in var, a fact which seems to be
overseen sometimes.
Cheers
Robert
路路路
On 7/27/06, rejoc <rejoc@freefree.fr> wrote:
Hello,
I new to Ruby and curious about this language I do use (yeet).
I was browsing through some libraries/modules and I found a line looking
like
a[:i] ||= "a string"
I could not find this syntax in any manual online (and googling "||=" is
not of much help )
Can anyone tell me what it means
TiA
--
Deux choses sont infinies : l'univers et la b锚tise humaine ; en ce qui
concerne l'univers, je n'en ai pas acquis la certitude absolue.
will return anotherObject (assuming it's value isn't nil)
The expression
a[:i] ||= "a string"
is equivalent to
a[:i] = a[:i] || "a string"
(sort of like 'num+= 1' is the same as 'num = num + 1')If a[:i] is not
nil then it will remain unchanged. If it is nil then it will be set to
"a string".