Hello,

Tom Sawyer <transami / transami.net> wrote:
> i've never seen an exact explination of ||= and i want to be clear about it.
> 
> 	x ||= y
> 
> means
> 
> 	x = x || y
> 
> which is equiv. to
> 
> 	x = x ? x : y
> 
> am i correct about this or is there some subtle aspect one may get caught on?

If x is not a variable, they are not equivalent.


 * Source:

class Foo
  def initialize
    @bar = 1
  end
  def bar=(x)
    p "bar= called"
    @bar = x
  end
  def bar
    p "bar called"
    @bar
  end
end

foo = Foo.new()
puts "ex.1  foo.bar ||= 1"
foo.bar ||= 1

puts
puts "ex.2  foo.bar = foo.bar || 1"
foo.bar = foo.bar || 1

puts
puts "ex.3  foo.bar = foo.bar ? foo.bar : 1"
foo.bar = foo.bar ? foo.bar : 1


 * Result:

ex.1  foo.bar ||= 1
"bar called"

ex.2  foo.bar = foo.bar || 1
"bar called"
"bar= called"

ex.3  foo.bar = foo.bar ? foo.bar : 1
"bar called"
"bar called"
"bar= called"


Regards,

TAKAHASHI 'Maki' Masayoshi     E-mail: maki / rubycolor.org