Newbie question

is in ruby operator for:

a = b if b

a (operator) b

?

···

--
Posted via http://www.ruby-forum.com/.

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

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

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:

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

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

"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

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/\.

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 - mój blog

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:

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)
  out = some_value if some_value
  puts("out: #{out.inspect}")
  out = nil || some_value
  puts("out: #{out.inspect}")
  out = some_value
  puts("out: #{out.inspect}")
  a_different_variable ||= some_value
  puts("a_different_variable: #{a_different_variable.inspect}")
  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/\.

------------
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)

$_ 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 $_)

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)
  out = some_value if some_value
  puts("out: #{out.inspect}")
  out = nil || some_value
  puts("out: #{out.inspect}")
  out = some_value
  puts("out: #{out.inspect}")
  a_different_variable ||= some_value
  puts("a_different_variable: #{a_different_variable.inspect}")
  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)

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 - mój blog