# Newbie question

**URL:** <https://rubytalk.org/t/newbie-question/45068>\
**Category:** ruby-talk\
**Created:** [10 March 2008 10:38 UTC](https://rubytalk.org/t/newbie-question/45068 "2008-03-10T10:38:48Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![Fero](https://avatars.discourse-cdn.com/v4/letter/f/b2d939/32.png) [@Fero](https://rubytalk.org/u/Fero)\
**Post date:** [10 March 2008 10:38 UTC](https://rubytalk.org/t/newbie-question/45068/1 "2008-03-10T10:38:48Z")

</div>

is in ruby operator for:

a = b if b

a (operator) b

?

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Farrel\_Lifson](https://avatars.discourse-cdn.com/v4/letter/f/3da27b/32.png) [@Farrel\_Lifson](https://rubytalk.org/u/Farrel_Lifson)\
**Post date:** [10 March 2008 10:50 UTC](https://rubytalk.org/t/newbie-question/45068/2 "2008-03-10T10:50:21Z")

</div>

a ||= b

Farrel

> **···**
>
> On 10/03/2008, Frantisek Psotka \<frantisek.psotka@matfyz.cz\> wrote:
> 
> > is in ruby operator for:
> > 
> > a = b if b
> > 
> > a (operator) b

---

<div class="post-metadata">

**Author:** ![Sebastian\_Hungereck1](https://avatars.discourse-cdn.com/v4/letter/s/3ab097/32.png) [@Sebastian\_Hungereck1](https://rubytalk.org/u/Sebastian_Hungereck1)\
**Post date:** [10 March 2008 11:02 UTC](https://rubytalk.org/t/newbie-question/45068/3 "2008-03-10T11:02:14Z")

</div>

Farrel Lifson wrote:

> **···**
>
> > On 10/03/2008, Frantisek Psotka \<frantisek.psotka@matfyz.cz\> wrote:
> 
> > \> a = b if b
> > 
> > a ||= b
> 
> Nope.  
> a ||= b is equivalent to a = a || b is equivalent to  
> a = if a then a else b end is NOT equivalent to a = b if b
> 
> HTH,  
> Sebastian  
> --  
> Jabber: sepp2k@jabber.org  
> ICQ: 205544826

---

<div class="post-metadata">

**Author:** ![Thomas\_Wieczorek](https://avatars.discourse-cdn.com/v4/letter/t/e47c2d/32.png) [@Thomas\_Wieczorek](https://rubytalk.org/u/Thomas_Wieczorek)\
**Post date:** [10 March 2008 11:03 UTC](https://rubytalk.org/t/newbie-question/45068/4 "2008-03-10T11:03:11Z")

</div>

> a ||= b

That's not right. It will give you a NameError, that b is undefined.  
a ||= b  
is equivalent to  
a = a || b  
which is the same as  
a = b if not a

> a = b if b

I know of no operator for it yet.

> **···**
>
> On Mon, Mar 10, 2008 at 11:50 AM, Farrel Lifson \<farrel.lifson@gmail.com\> wrote:

---

<div class="post-metadata">

**Author:** ![Jan\_Svitok](https://avatars.discourse-cdn.com/v4/letter/j/cab0a1/32.png) [@Jan\_Svitok](https://rubytalk.org/u/Jan_Svitok)\
**Post date:** [10 March 2008 11:04 UTC](https://rubytalk.org/t/newbie-question/45068/5 "2008-03-10T11:04:42Z")

</div>

a ||= b is

a = a || b  
that is  
a = b if !a  
that is  
a = b unless a

It is equal to "a = b if b" if both a and b are of boolean type  
(doesn't work for e.g. numbers).

> **···**
>
> On Mon, Mar 10, 2008 at 11:50 AM, Farrel Lifson \<farrel.lifson@gmail.com\> wrote:
> 
> > On 10/03/2008, Frantisek Psotka \<frantisek.psotka@matfyz.cz\> wrote:  
> > \> is in ruby operator for:  
> > \>  
> > \> a = b if b  
> > \>  
> > \> a (operator) b
> > 
> > a ||= b
> > 
> > Farrel

---

<div class="post-metadata">

**Author:** ![Alex\_Young](https://avatars.discourse-cdn.com/v4/letter/a/ee7513/32.png) [@Alex\_Young](https://rubytalk.org/u/Alex_Young)\
**Post date:** [10 March 2008 11:08 UTC](https://rubytalk.org/t/newbie-question/45068/6 "2008-03-10T11:08:23Z")

</div>

Not quite:

irb(main):007:0\> a = 1  
=\> 1  
irb(main):008:0\> b = 2  
=\> 2  
irb(main):009:0\> a ||= b  
=\> 1  
irb(main):010:0\>  
irb(main):011:0\* a  
=\> 1  
irb(main):012:0\> a = b if b  
=\> 2  
irb(main):013:0\> a  
=\> 2

a ||= b is equivalent to a = b if !a, which isn't quite the same thing  
as a = b if b.

> **···**
>
> On Mon, 2008-03-10 at 19:50 +0900, Farrel Lifson wrote:
> 
> > On 10/03/2008, Frantisek Psotka \<frantisek.psotka@matfyz.cz\> wrote:  
> > \> is in ruby operator for:  
> > \>  
> > \> a = b if b  
> > \>  
> > \> a (operator) b
> > 
> > a ||= b
> 
> --  
> Alex

---

<div class="post-metadata">

**Author:** ![Lars](https://avatars.discourse-cdn.com/v4/letter/l/74df32/32.png) [@Lars](https://rubytalk.org/u/Lars)\
**Post date:** [10 March 2008 11:11 UTC](https://rubytalk.org/t/newbie-question/45068/7 "2008-03-10T11:11:10Z")

</div>

"a ||= b" means "a = b unless a".

I don't think there is an operator for "a = b if b".

Luckily, "a = b if b" is valid ruby code. "a = b || a" or "a = (b or  
a)" would also work.

> **···**
>
> On Mar 10, 11:50 am, Farrel Lifson \<farrel.lif...@gmail.com\> wrote:
> 
> > On 10/03/2008, Frantisek Psotka \<frantisek.pso...@matfyz.cz\> wrote:
> > 
> > \> is in ruby operator for:
> > 
> > \> a = b if b
> > 
> > \> a (operator) b
> > 
> > a ||= b

---

<div class="post-metadata">

**Author:** ![Fero](https://avatars.discourse-cdn.com/v4/letter/f/b2d939/32.png) [@Fero](https://rubytalk.org/u/Fero)\
**Post date:** [10 March 2008 14:11 UTC](https://rubytalk.org/t/newbie-question/45068/8 "2008-03-10T14:11:54Z")

</div>

yes, i can write

a = b || a

but isn't it more complex. imagine that b is nil. then a = a will be  
evaluated?

maybe time for feature request? new operator a =|| (a = b if b)

(variable = params[:nice\_symbol] if params[:nice\_symbol] doesnt look  
very nice)

Lars wrote:

> **···**
>
> > On Mar 10, 11:50 am, Farrel Lifson \<farrel.lif...@gmail.com\> wrote:
> > 
> > > On 10/03/2008, Frantisek Psotka \<frantisek.pso...@matfyz.cz\> wrote:
> > > 
> > > \> is in ruby operator for:
> > > 
> > > \> a = b if b
> > > 
> > > \> a (operator) b
> > > 
> > > a ||= b
> > 
> > "a ||= b" means "a = b unless a".
> > 
> > I don't think there is an operator for "a = b if b".
> > 
> > Luckily, "a = b if b" is valid ruby code. "a = b || a" or "a = (b or  
> > a)" would also work.
> 
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Radoslaw\_Bulat](https://avatars.discourse-cdn.com/v4/letter/r/71c47a/32.png) [@Radoslaw\_Bulat](https://rubytalk.org/u/Radoslaw_Bulat)\
**Post date:** [10 March 2008 15:04 UTC](https://rubytalk.org/t/newbie-question/45068/9 "2008-03-10T15:04:53Z")

</div>

And after x years later we would have another Perl. I don't like to  
have gazillions operators for all occasions.  
a = b if b is that hard to write and read? I don't think so :).  
Direction for Ruby future should be to clean those ugly perlism from  
Ruby (for example magic variable $\_), rather that to add new stuff  
like that.

> **···**
>
> On Mon, Mar 10, 2008 at 3:11 PM, Frantisek Psotka \<frantisek.psotka@matfyz.cz\> wrote:
> 
> > yes, i can write
> > 
> > a = b || a
> > 
> > but isn't it more complex. imagine that b is nil. then a = a will be  
> > evaluated?
> > 
> > maybe time for feature request? new operator a =|| (a = b if b)
> > 
> > (variable = params[:nice\_symbol] if params[:nice\_symbol] doesnt look  
> > very nice)
> 
> --  
> Radosław Bułat
> 
> [http://radarek.jogger.pl](http://radarek.jogger.pl) - mój blog

---

<div class="post-metadata">

**Author:** ![Christopher\_Dicely](https://avatars.discourse-cdn.com/v4/letter/c/7c8e57/32.png) [@Christopher\_Dicely](https://rubytalk.org/u/Christopher_Dicely)\
**Post date:** [11 March 2008 03:30 UTC](https://rubytalk.org/t/newbie-question/45068/10 "2008-03-11T03:30:21Z")

</div>

> yes, i can write
> 
> a = b || a
> 
> but isn't it more complex. imagine that b is nil. then a = a will be  
> evaluated?

Then why not just "a = b if b"?

> maybe time for feature request? new operator a =|| (a = b if b)
> 
> (variable = params[:nice\_symbol] if params[:nice\_symbol] doesnt look  
> very nice)

Doesn't bother me, its a lot more clear than variable =|| params[:nice\_symbol].  
More operators that look similar and have similar but subtly different meanings  
is less immediate visual clarity.

> **···**
>
> On Mon, Mar 10, 2008 at 7:11 AM, Frantisek Psotka \<frantisek.psotka@matfyz.cz\> wrote:

---

<div class="post-metadata">

**Author:** ![Ben\_Oakes](https://avatars.discourse-cdn.com/v4/letter/b/b5ac83/32.png) [@Ben\_Oakes](https://rubytalk.org/u/Ben_Oakes)\
**Post date:** [11 March 2008 11:18 UTC](https://rubytalk.org/t/newbie-question/45068/11 "2008-03-11T11:18:12Z")

</div>

I guess I don't understand what the point of having a = b if b is.  
The only thing I can see that's different than a ||= b is that false  
will become nil. Here's what I tried:

def demo(some\_value)  
&nbsp;&nbsp;out = some\_value if some\_value  
&nbsp;&nbsp;puts("out: #{out.inspect}")  
&nbsp;&nbsp;out = nil || some\_value  
&nbsp;&nbsp;puts("out: #{out.inspect}")  
&nbsp;&nbsp;out = some\_value  
&nbsp;&nbsp;puts("out: #{out.inspect}")  
&nbsp;&nbsp;a\_different\_variable ||= some\_value  
&nbsp;&nbsp;puts("a\_different\_variable: #{a\_different\_variable.inspect}")  
&nbsp;&nbsp;puts('------------')  
end

demo(nil)  
demo(88)  
demo(false)

Outputs:

out: nil  
out: nil  
out: nil  
a\_different\_variable: nil

> **···**
>
> On Mon, Mar 10, 2008 at 9:11 AM, Frantisek Psotka \<frantisek.psotka@matfyz.cz\> wrote:
> 
> > yes, i can write
> > 
> > a = b || a
> > 
> > but isn't it more complex. imagine that b is nil. then a = a will be  
> > evaluated?
> > 
> > maybe time for feature request? new operator a =|| (a = b if b)
> > 
> > (variable = params[:nice\_symbol] if params[:nice\_symbol] doesnt look  
> > very nice)
> > 
> > Lars wrote:  
> > \> On Mar 10, 11:50 am, Farrel Lifson \<farrel.lif...@gmail.com\> wrote:  
> > \>\> On 10/03/2008, Frantisek Psotka \<frantisek.pso...@matfyz.cz\> wrote:  
> > \>\>  
> > \>\> \> is in ruby operator for:  
> > \>\>  
> > \>\> \> a = b if b  
> > \>\>  
> > \>\> \> a (operator) b  
> > \>\>  
> > \>\> a ||= b  
> > \>  
> > \> "a ||= b" means "a = b unless a".  
> > \>  
> > \> I don't think there is an operator for "a = b if b".  
> > \>  
> > \> Luckily, "a = b if b" is valid ruby code. "a = b || a" or "a = (b or  
> > \> a)" would also work.
> > 
> > --  
> > Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).
> 
> ------------  
> out: 88  
> out: 88  
> out: 88  
> a\_different\_variable: 88  
> ------------  
> out: nil  
> out: false  
> out: false  
> a\_different\_variable: false  
> ------------
> 
> So... Why doesn't ||= work for how you're using it? (Why do you want  
> the "b if b" behavior?)
> 
> -- Ben (aka divokz)

---

<div class="post-metadata">

**Author:** ![W\_James](https://avatars.discourse-cdn.com/v4/letter/w/e274bd/32.png) [@W\_James](https://rubytalk.org/u/W_James)\
**Post date:** [11 March 2008 04:45 UTC](https://rubytalk.org/t/newbie-question/45068/12 "2008-03-11T04:45:09Z")

</div>

$\_ is no more magic than ARGV.

Paul Graham's new language Arc uses \_ for a similar purpose.

> **···**
>
> On Mar 10, 9:04 am, Radosław Bułat \<radek.bu...@gmail.com\> wrote:
> 
> > Direction for Ruby future should be to clean those ugly perlism from  
> > Ruby (for example magic variable $\_)

---

<div class="post-metadata">

**Author:** ![Todd\_Benson](https://avatars.discourse-cdn.com/v4/letter/t/34f0e0/32.png) [@Todd\_Benson](https://rubytalk.org/u/Todd_Benson)\
**Post date:** [11 March 2008 17:44 UTC](https://rubytalk.org/t/newbie-question/45068/13 "2008-03-11T17:44:18Z")

</div>

Because your first test will be different if "out" already exists and  
is not nil. The OP wants "out" to change only if "some\_variable" is  
not nil.

Todd

> **···**
>
> On Tue, Mar 11, 2008 at 6:18 AM, Benjamin Oakes \<benjamin.d.oakes@gmail.com\> wrote:
> 
> > I guess I don't understand what the point of having a = b if b is.  
> > The only thing I can see that's different than a ||= b is that false  
> > will become nil. Here's what I tried:
> > 
> > def demo(some\_value)  
> > &nbsp;&nbsp;out = some\_value if some\_value  
> > &nbsp;&nbsp;puts("out: #{out.inspect}")  
> > &nbsp;&nbsp;out = nil || some\_value  
> > &nbsp;&nbsp;puts("out: #{out.inspect}")  
> > &nbsp;&nbsp;out = some\_value  
> > &nbsp;&nbsp;puts("out: #{out.inspect}")  
> > &nbsp;&nbsp;a\_different\_variable ||= some\_value  
> > &nbsp;&nbsp;puts("a\_different\_variable: #{a\_different\_variable.inspect}")  
> > &nbsp;&nbsp;puts('------------')  
> > end
> > 
> > demo(nil)  
> > demo(88)  
> > demo(false)
> > 
> > Outputs:
> > 
> > out: nil  
> > out: nil  
> > out: nil  
> > a\_different\_variable: nil  
> > ------------  
> > out: 88  
> > out: 88  
> > out: 88  
> > a\_different\_variable: 88  
> > ------------  
> > out: nil  
> > out: false  
> > out: false  
> > a\_different\_variable: false  
> > ------------
> > 
> > So... Why doesn't ||= work for how you're using it? (Why do you want  
> > the "b if b" behavior?)
> > 
> > -- Ben (aka divokz)

---

<div class="post-metadata">

**Author:** ![Radoslaw\_Bulat](https://avatars.discourse-cdn.com/v4/letter/r/71c47a/32.png) [@Radoslaw\_Bulat](https://rubytalk.org/u/Radoslaw_Bulat)\
**Post date:** [12 March 2008 13:49 UTC](https://rubytalk.org/t/newbie-question/45068/14 "2008-03-12T13:49:29Z")

</div>

I don't get it. ARGV is for program parameters. All languages have it  
but of course maybe in different way (like java String args in main  
method). Where is magic here?

> **···**
>
> 2008/3/11 William James \<w\_a\_x\_man@yahoo.com\>:
> 
> > On Mar 10, 9:04 am, Radosław Bułat \<radek.bu...@gmail.com\> wrote:
> > 
> > \> Direction for Ruby future should be to clean those ugly perlism from  
> > \> Ruby (for example magic variable $\_)
> > 
> > $\_ is no more magic than ARGV.
> 
> --  
> Radosław Bułat
> 
> [http://radarek.jogger.pl](http://radarek.jogger.pl) - mój blog

---

<div class="post-metadata">

**Author:** ![Ben\_Oakes](https://avatars.discourse-cdn.com/v4/letter/b/b5ac83/32.png) [@Ben\_Oakes](https://rubytalk.org/u/Ben_Oakes)\
**Post date:** [12 March 2008 14:32 UTC](https://rubytalk.org/t/newbie-question/45068/15 "2008-03-12T14:32:54Z")

</div>


