is in ruby operator for:
a = b if b
a (operator) b
?
is in ruby operator for:
a = b if b
a (operator) b
?
On 10/03/2008, Frantisek P. [email protected] wrote:
is in ruby operator for:
a = b if b
a (operator) b
a ||= b
Farrel
Farrel L. wrote:
On 10/03/2008, Frantisek P. [email protected] 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
On Mon, Mar 10, 2008 at 11:50 AM, Farrel L.
[email protected] wrote:
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 L.
[email protected] wrote:
On 10/03/2008, Frantisek P. [email protected] wrote:
is in ruby operator for:
a = b if b
a (operator) b
a ||= b
Farrel
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, 2008-03-10 at 19:50 +0900, Farrel L. wrote:
On 10/03/2008, Frantisek P. [email protected] wrote:
is in ruby operator for:
a = b if b
a (operator) b
a ||= b
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 Mar 10, 11:50 am, Farrel L. [email protected] wrote:
On 10/03/2008, Frantisek P. [email protected] 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.
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 L. [email protected] wrote:
On 10/03/2008, Frantisek P. [email protected] 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.
On Mon, Mar 10, 2008 at 7:11 AM, Frantisek P.
[email protected] 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?
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 Mar 10, 9:04 am, Rados³aw Bu³at [email protected] wrote:
Direction for Ruby future should be to clean those ugly perlism from
Ruby (for example magic variable $_)
$_ is no more magic than ARGV.
Paul Graham’s new language Arc uses _ for a similar purpose.
On Mon, Mar 10, 2008 at 3:11 PM, Frantisek P.
[email protected] wrote:
very nice)
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.
–
Rados³aw Bu³at
http://radarek.jogger.pl - mój blog
On Tue, Mar 11, 2008 at 6:18 AM, Benjamin Oakes
[email protected] wrote:
puts(“out: #{out.inspect}”)
out: nil
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 Mon, Mar 10, 2008 at 9:11 AM, Frantisek P.
[email protected] wrote:
very nice)
a = b if b
a)" would also work.–
Posted via http://www.ruby-forum.com/.
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:
So… Why doesn’t ||= work for how you’re using it? (Why do you want
the “b if b” behavior?)
– Ben (aka divokz)
2008/3/11 William J. [email protected]:
On Mar 10, 9:04 am, Rados³aw Bu³at [email protected] wrote:
Direction for Ruby future should be to clean those ugly perlism from
Ruby (for example magic variable $_)$_ is no more magic than ARGV.
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?
–
Rados³aw Bu³at
http://radarek.jogger.pl - mój blog
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs