Assigning to hash keys when there is a default value?

Hi --

I for one, am glad that it works this way. The ruby idiom

x ||= y

is heavily used for lazy initialization/caching. While most often,
it's the rhs which is expensive to compute and therefore the thing we
want to short-circuit, since x= can in general be a method, and might
just be expensive, then optimizing the case where it boils down to x =
x as a nop, makes sense.

I think The method always gets called, though:

   class C
     attr_reader :x
     def x=(n)
       puts "C#x="
       true
     end
   end

   c = C.new
   c.x ||= 3 # C#x=

*Unless*, of course, the object is a Hash which has either (a) a key
corresponding to the indicated value, or (b) a default value with
boolean truth value.

No, in the case you posited, the assignment happened and C#x= got
called because c.x returned nil.

Right; I got that wrong.

Sigh. I really wish it were otherwise. What an annoying exception to
the rule.

Except that the 'rule' wasn't as you thought. The rule is that

   x ||= y

is the same as

  x = y unless x

OK, then: What an annoying exception to what should be the rule :slight_smile:

Also, in the famous:

   h = Hash.new(1)
   h[5] ||= 10

case, it definitely isn't doing the "x = x" equivalent, since that
would set the 5 key to 1.

And that's as expected because h[5] returns the default value and
doesn't affect the state of the hash a whit, it doesn't create a 5
key. If you want the default to affect the hash you need something
like

hsh = Hash.new {|h,k| h[k] = 10}

That one I didn't get wrong :slight_smile: I didn't say that retrieving the
default value creates a key (which it doesn't, since the default
value, whether nil or what you set it to, is specifically the default
value for keys that don't exist). My point was that this:

   h = Hash.new(1)
   h[5] ||= 10

does not map to "x = x", assuming that x stands for h[5]. h[5] = h[5]
*does* set a key; as I said, it would set the 5 key to 1. In fact
this whole thread is really about the fact that hash defaults, which
don't set keys, can be true, which short-circuits the ||= thing. I do
think it's the only such case, and probably fairly edge, though
obviously I'd like to see it do otherwise.

David

···

On Wed, 5 Sep 2007, Rick DeNatale wrote:

On 9/4/07, dblack@wobblini.net <dblack@wobblini.net> wrote:

--
* Books:
   RAILS ROUTING (new! http://www.awprofessional.com/title/0321509242\)
   RUBY FOR RAILS (http://www.manning.com/black\)
* Ruby/Rails training
     & consulting: Ruby Power and Light, LLC (http://www.rubypal.com)

Hi --

···

On Mon, 3 Sep 2007, Morton Goldberg wrote:

On Sep 3, 2007, at 7:37 AM, dblack@wobblini.net wrote:

The only possible explanation I can come up with, which I don't like,
is that:

h[2] ||= 10

is being treated like:

5 = 5 || 10

which is just another can of worms.

I don't want to believe that because it raises the question of why

h[2] = 10

isn't treated as

5 = 10

I mean, I simply can't believe the Ruby interpreter would treat h[2] as a simple rvalue in h[2] ||= 10.

I agree -- it definitely wouldn't. It was a kind of reductio ad
absurdum.

David

--
* Books:
   RAILS ROUTING (new! http://www.awprofessional.com/title/0321509242\)
   RUBY FOR RAILS (http://www.manning.com/black\)
* Ruby/Rails training
     & consulting: Ruby Power and Light, LLC (http://www.rubypal.com)

I learned that x ||= y means set x to y unless x, so I don't see the
bug. and i don't see this as being at all unpredictable. either way
you expect it to work, it either fails 100% or succeeds 100%. :slight_smile: the
only problem is when you expect it to do something it doesn't do,
which is tautologically what every problem is i guess.

RSL

···

On 9/3/07, dblack@wobblini.net <dblack@wobblini.net> wrote:

Hi --

On Mon, 3 Sep 2007, Russell Norris wrote:

> I don't think this is a bug, kittens. since h[2] returns a value [even
> though it's not set], it causes h[2] to evaluate so the assignment
> never happens. x ||= y just means give me x or set x to y if there's
> no value for x. h[2] _does_ have a value if only a default one.
>
> just my two cents.
>
> unless i missed the point here, in which case i apologize for my hasty
> conclusion. :))

The thing is, this:

   x ||= y

always means (at least, so I've always been told):

   x = x || y

which in the case of the hash with a default value of 5, would mean:

   h[2] = 5 || 10

In other words: h.=(2, 5||10)

At that point, the default value is out of the picture. The default
value has no implications for the #= (writer) method; it's only what
you get when you use the # (reader) method.

So what's at stake here is the matter of x ||= y behaving predictably.
I have to say, it's a case where I'm not even really concerned with
the implementation (e.g., whether it uses on its way to =) but
just the semantics. I don't like the idea that the syntactic sugar is
actually not a reliable drop-in replacement for the thing it's
sugaring.

David

--
* Books:
   RAILS ROUTING (new! http://www.awprofessional.com/title/0321509242\)
   RUBY FOR RAILS (http://www.manning.com/black\)
* Ruby/Rails training
     & consulting: Ruby Power and Light, LLC (http://www.rubypal.com)

I believe it's not the only case. Although x= and = are different, it seems in *both* cases assignment is not even invoked for ||=:

irb(main):001:0> class Foo
irb(main):002:1> def x
irb(main):003:2> p "x"
irb(main):004:2> @x
irb(main):005:2> end
irb(main):006:1> def x=(v)
irb(main):007:2> p "x="
irb(main):008:2> @x=v
irb(main):009:2> end
irb(main):010:1> def (k)
irb(main):011:2> p ""
irb(main):012:2> k
irb(main):013:2> end
irb(main):014:1> def =(k,v)
irb(main):015:2> p "="
irb(main):016:2> k
irb(main):017:2> end
irb(main):018:1> end
=> nil
irb(main):019:0> f=Foo.new
=> #<Foo:0x7ff6b5c4>
irb(main):020:0> f.x||=10
"x"
"x="
=> 10
irb(main):021:0> f.x||=20
"x"
=> 10
irb(main):022:0> f.x=30
"x="
=> 30
irb(main):023:0> f[10]||=20
""
=> 10
irb(main):024:0> f[nil]||=30
""
"="
=> 30
irb(main):025:0> f[20]=30
"="
=> 30
irb(main):026:0>

Kind regards

  robert

···

On 05.09.2007 00:15, dblack@wobblini.net wrote:

Hi --

On Wed, 5 Sep 2007, Rick DeNatale wrote:

On 9/4/07, dblack@wobblini.net <dblack@wobblini.net> wrote:

I for one, am glad that it works this way. The ruby idiom

x ||= y

is heavily used for lazy initialization/caching. While most often,
it's the rhs which is expensive to compute and therefore the thing we
want to short-circuit, since x= can in general be a method, and might
just be expensive, then optimizing the case where it boils down to x =
x as a nop, makes sense.

I think The method always gets called, though:

   class C
     attr_reader :x
     def x=(n)
       puts "C#x="
       true
     end
   end

   c = C.new
   c.x ||= 3 # C#x=

*Unless*, of course, the object is a Hash which has either (a) a key
corresponding to the indicated value, or (b) a default value with
boolean truth value.

No, in the case you posited, the assignment happened and C#x= got
called because c.x returned nil.

Right; I got that wrong.

Sigh. I really wish it were otherwise. What an annoying exception to
the rule.

Except that the 'rule' wasn't as you thought. The rule is that

   x ||= y

is the same as

  x = y unless x

OK, then: What an annoying exception to what should be the rule :slight_smile:

Also, in the famous:

   h = Hash.new(1)
   h[5] ||= 10

case, it definitely isn't doing the "x = x" equivalent, since that
would set the 5 key to 1.

And that's as expected because h[5] returns the default value and
doesn't affect the state of the hash a whit, it doesn't create a 5
key. If you want the default to affect the hash you need something
like

hsh = Hash.new {|h,k| h[k] = 10}

That one I didn't get wrong :slight_smile: I didn't say that retrieving the
default value creates a key (which it doesn't, since the default
value, whether nil or what you set it to, is specifically the default
value for keys that don't exist). My point was that this:

  h = Hash.new(1)
  h[5] ||= 10

does not map to "x = x", assuming that x stands for h[5]. h[5] = h[5]
*does* set a key; as I said, it would set the 5 key to 1. In fact
this whole thread is really about the fact that hash defaults, which
don't set keys, can be true, which short-circuits the ||= thing. I do
think it's the only such case, and probably fairly edge, though
obviously I'd like to see it do otherwise.

Hi --

···

On Mon, 3 Sep 2007, Russell Norris wrote:

I learned that x ||= y means set x to y unless x, so I don't see the
bug. and i don't see this as being at all unpredictable. either way
you expect it to work, it either fails 100% or succeeds 100%. :slight_smile: the
only problem is when you expect it to do something it doesn't do,
which is tautologically what every problem is i guess.

I guess one can just accomodate oneself to whatever happens, in which
case there are no bugs... but somehow it never seems to play out that
way :slight_smile: Anyway, we're going around in circles. I think I might hop
over to ruby-core and see what people think.

David

--
* Books:
   RAILS ROUTING (new! http://www.awprofessional.com/title/0321509242\)
   RUBY FOR RAILS (http://www.manning.com/black\)
* Ruby/Rails training
     & consulting: Ruby Power and Light, LLC (http://www.rubypal.com)

# I learned that x ||= y means set x to y unless x, so I don't see the

there's the bug: you've changed the meaning of x ||= y, or of foo<op>=bar for that matter.

# bug. and i don't see this as being at all unpredictable.

you just said you've _learned that x||=y means set x to y unless x. Surely it was _unpredictable at some point. Surely x<op>=y is a no-brainer for a many of us :slight_smile:

and now we all have to update our test cases, cause maybe x += 1 may not always be x = x + 1

speaking of least surprise :frowning:

kind regards -botp

···

From: sconds@gmail.com [mailto:sconds@gmail.com] On Behalf Of Russell Norris:

Hi --

That one I didn't get wrong :slight_smile: I didn't say that retrieving the
default value creates a key (which it doesn't, since the default
value, whether nil or what you set it to, is specifically the default
value for keys that don't exist). My point was that this:

  h = Hash.new(1)
  h[5] ||= 10

does not map to "x = x", assuming that x stands for h[5]. h[5] = h[5]
*does* set a key; as I said, it would set the 5 key to 1. In fact
this whole thread is really about the fact that hash defaults, which
don't set keys, can be true, which short-circuits the ||= thing. I do
think it's the only such case, and probably fairly edge, though
obviously I'd like to see it do otherwise.

I believe it's not the only case. Although x= and = are different, it seems in *both* cases assignment is not even invoked for ||=:

True, but by "only *such* case" I meant: only case where evaluating
the lhs and getting true doesn't tell the whole story, in terms of the
functionality of the object, because of the defaulting to a value in
the absence of a key.

I suppose one could argue that it isn't ||='s problem if the lhs is
actually a kind of proxy. Nothing has yet convinced me, though, that

= should not behave like x = x || y, even if the actual assignment

gets optimized away.

David

···

On Fri, 7 Sep 2007, Robert Klemme wrote:

On 05.09.2007 00:15, dblack@wobblini.net wrote:

--
* Books:
   RAILS ROUTING (new! http://www.awprofessional.com/title/0321509242\)
   RUBY FOR RAILS (http://www.manning.com/black\)
* Ruby/Rails training
     & consulting: Ruby Power and Light, LLC (http://www.rubypal.com)

Will be interesting to hear what they say.

In the meantime: I believe we're discussing something that's not an
issue most of the time, because even if there is no assignment with
the default value, querying will still return the same result. The
only possible issue here is that Hash#keys returns something different
depending on the implementation.

Why do I think there is no problem most of the time? Let's look at
some typical idioms which work as we like them to work:

counters = Hash.new 0
...
counters[item] += 1

counters.each do |key,value|
  printf "%5d %s\n", value, key
end

lists = Hash.new {|h,k| h[k] = }
...
lists[key] << item

lists.each do |key,list|
  print key, " ", list.inspect, "\n"
end

I wonder where we would actually use ||= with a Hash. Is this a
realistic example?

found = Hash.new
...
found[item] ||= true

I don't think so, because in that case I'd rather use

found[item] = true

Is ||= really used with Hash at all? I can't think of a case ATM but
that might just be my limited fantasy. :slight_smile:

Kind regards

robert

···

2007/9/3, dblack@wobblini.net <dblack@wobblini.net>:

Hi --

On Mon, 3 Sep 2007, Russell Norris wrote:

> I learned that x ||= y means set x to y unless x, so I don't see the
> bug. and i don't see this as being at all unpredictable. either way
> you expect it to work, it either fails 100% or succeeds 100%. :slight_smile: the
> only problem is when you expect it to do something it doesn't do,
> which is tautologically what every problem is i guess.

I guess one can just accomodate oneself to whatever happens, in which
case there are no bugs... but somehow it never seems to play out that
way :slight_smile: Anyway, we're going around in circles. I think I might hop
over to ruby-core and see what people think.

Ahhh now I see what you kittens are seeing. I _still_ don't think it's
a bug though. <op>|| might be a no-brainer but ||= isn't completely
analogous to methods like += since || itself isn't a method though it
is an operator. || always returns either/or. And in this case, I
really think the operator is doing the expected behavior even if the
<op>= might be a misleading analogy.

Also, I didn't mean that I learned from experience but that it was the
way I was taught this. In case that makes a difference. I dunno.

Finally, I musta missed something else 'cause x += 1 [unless x isn't
numeric to begin with] _always_ increments and you said that it might
not always do so. Apologies if I've missed some joke or something. :slight_smile:

RSL

···

On 9/3/07, Peña, Botp <botp@delmonte-phil.com> wrote:

From: sconds@gmail.com [mailto:sconds@gmail.com] On Behalf Of Russell Norris:
# I learned that x ||= y means set x to y unless x, so I don't see the

there's the bug: you've changed the meaning of x ||= y, or of foo<op>=bar for that matter.

# bug. and i don't see this as being at all unpredictable.

you just said you've _learned that x||=y means set x to y unless x. Surely it was _unpredictable at some point. Surely x<op>=y is a no-brainer for a many of us :slight_smile:

and now we all have to update our test cases, cause maybe x += 1 may not always be x = x + 1

speaking of least surprise :frowning:

kind regards -botp

No, "x+=1" will always be "x=x+1".

robert

···

2007/9/4, Peña, Botp <botp@delmonte-phil.com>:

From: sconds@gmail.com [mailto:sconds@gmail.com] On Behalf Of Russell Norris:
# I learned that x ||= y means set x to y unless x, so I don't see the

there's the bug: you've changed the meaning of x ||= y, or of foo<op>=bar for that matter.

# bug. and i don't see this as being at all unpredictable.

you just said you've _learned that x||=y means set x to y unless x. Surely it was _unpredictable at some point. Surely x<op>=y is a no-brainer for a many of us :slight_smile:

and now we all have to update our test cases, cause maybe x += 1 may not always be x = x + 1

speaking of least surprise :frowning:

> I believe it's not the only case. Although x= and = are different, it
> seems in *both* cases assignment is not even invoked for ||=:

True, but by "only *such* case" I meant: only case where evaluating
the lhs and getting true doesn't tell the whole story, in terms of the
functionality of the object, because of the defaulting to a value in
the absence of a key.

Ah, ok. Should've read more carefully. I am sorry.

I suppose one could argue that it isn't ||='s problem if the lhs is
actually a kind of proxy. Nothing has yet convinced me, though, that
>>= should not behave like x = x || y, even if the actual assignment
gets optimized away.

Umm, now I am confused. I thought the "optimized away" bit is the
critical bit - if you allow for this difference then they do actually
behave the same, don't they?

Btw, here's a possible explanation why the behavior is the way it is:
= and x= are usually costly operations (i.e. not just assignments
but methods doing some work) so avoiding that would help overall
performance. Still I believe that's probably better than the
consistency although I usually tend to favor consistency as well.

Kind regards

robert

···

2007/9/7, dblack@wobblini.net <dblack@wobblini.net>:

If '"x+=1" will always be "x=x+1"', what's the problem in having x ||=
1 always be x = x || 1?

···

On Sep 4, 7:32 am, "Robert Klemme" <shortcut...@googlemail.com> wrote:

2007/9/4, Peña, Botp <b...@delmonte-phil.com>:

> From: sco...@gmail.com [mailto:sco...@gmail.com] On Behalf Of Russell Norris:
> # I learned that x ||= y means set x to y unless x, so I don't see the

> there's the bug: you've changed the meaning of x ||= y, or of foo<op>=bar for that matter.

> # bug. and i don't see this as being at all unpredictable.

> you just said you've _learned that x||=y means set x to y unless x. Surely it was _unpredictable at some point. Surely x<op>=y is a no-brainer for a many of us :slight_smile:

> and now we all have to update our test cases, cause maybe x += 1 may not always be x = x + 1

> speaking of least surprise :frowning:

No, "x+=1" will always be "x=x+1".

robert

--
-yossef

Hi --

I believe it's not the only case. Although x= and = are different, it
seems in *both* cases assignment is not even invoked for ||=:

True, but by "only *such* case" I meant: only case where evaluating
the lhs and getting true doesn't tell the whole story, in terms of the
functionality of the object, because of the defaulting to a value in
the absence of a key.

Ah, ok. Should've read more carefully. I am sorry.

I suppose one could argue that it isn't ||='s problem if the lhs is
actually a kind of proxy. Nothing has yet convinced me, though, that
>>= should not behave like x = x || y, even if the actual assignment
gets optimized away.

Umm, now I am confused. I thought the "optimized away" bit is the
critical bit - if you allow for this difference then they do actually
behave the same, don't they?

That may be right. I guess I'm focusing on the visible behavior, and
not the implementation, so I'm probably saying dumb things about the
implementation.

Btw, here's a possible explanation why the behavior is the way it is:
= and x= are usually costly operations (i.e. not just assignments
but methods doing some work) so avoiding that would help overall
performance. Still I believe that's probably better than the
consistency although I usually tend to favor consistency as well.

That was Rick DeNatale's point too: that it could be expensive to call
a =-method. I agree, though if x[1] ||= y were just sugar for x[1] =
x[1] || y, then one could optimize it on the Ruby side with x[1] = y
unless x[1].

Oh well. I don't think this is going to change, and at this point I
should probably stop complaining unless I can come up with a new
implementation :slight_smile:

David

···

On Fri, 7 Sep 2007, Robert Klemme wrote:

2007/9/7, dblack@wobblini.net <dblack@wobblini.net>:

--
* Books:
   RAILS ROUTING (new! http://www.awprofessional.com/title/0321509242\)
   RUBY FOR RAILS (http://www.manning.com/black\)
* Ruby/Rails training
     & consulting: Ruby Power and Light, LLC (http://www.rubypal.com)

The problem with that is that || doesn't work that way, imo.

x || whatever

only does whatever if x isn't true. Excuse me if I'm sounding like a
broken record but the more we talk about this the more I'm convinced
that this "bug" only exists if you expect || to act like + does just
because it's an "operator" instead of allowing for the fact that ||
isn't a method like + and friends.

is special. It takes another bus to ||= than + does, heh. :wink:

Sorry for the silliness there.

RSL

···

On 9/4/07, Yossef Mendelssohn <ymendel@pobox.com> wrote:

On Sep 4, 7:32 am, "Robert Klemme" <shortcut...@googlemail.com> wrote:
> 2007/9/4, Peña, Botp <b...@delmonte-phil.com>:
>
> > From: sco...@gmail.com [mailto:sco...@gmail.com] On Behalf Of Russell Norris:
> > # I learned that x ||= y means set x to y unless x, so I don't see the
>
> > there's the bug: you've changed the meaning of x ||= y, or of foo<op>=bar for that matter.
>
> > # bug. and i don't see this as being at all unpredictable.
>
> > you just said you've _learned that x||=y means set x to y unless x. Surely it was _unpredictable at some point. Surely x<op>=y is a no-brainer for a many of us :slight_smile:
>
> > and now we all have to update our test cases, cause maybe x += 1 may not always be x = x + 1
>
> > speaking of least surprise :frowning:
>
> No, "x+=1" will always be "x=x+1".
>
> robert

If '"x+=1" will always be "x=x+1"', what's the problem in having x ||=
1 always be x = x || 1?

--
-yossef

That an assignment of x=x is useless (basically a nop in the standard
case of x being a variable). Please see also one of my earlier
postings.

Kind regards

robert

···

2007/9/4, Yossef Mendelssohn <ymendel@pobox.com>:

On Sep 4, 7:32 am, "Robert Klemme" <shortcut...@googlemail.com> wrote:
> 2007/9/4, Peña, Botp <b...@delmonte-phil.com>:
>
> > From: sco...@gmail.com [mailto:sco...@gmail.com] On Behalf Of Russell Norris:
> > # I learned that x ||= y means set x to y unless x, so I don't see the
>
> > there's the bug: you've changed the meaning of x ||= y, or of foo<op>=bar for that matter.
>
> > # bug. and i don't see this as being at all unpredictable.
>
> > you just said you've _learned that x||=y means set x to y unless x. Surely it was _unpredictable at some point. Surely x<op>=y is a no-brainer for a many of us :slight_smile:
>
> > and now we all have to update our test cases, cause maybe x += 1 may not always be x = x + 1
>
> > speaking of least surprise :frowning:
>
> No, "x+=1" will always be "x=x+1".
>
> robert

If '"x+=1" will always be "x=x+1"', what's the problem in having x ||=
1 always be x = x || 1?

I wonder why nobody commented on my attempt to point at the practical
implications. Sure I understand that it would be more consistent if
x>>=y were equivalent to x = x||y but there are two questions here
IMHO that need to be answered: 1. which solution has advantages in
practice and 2. does it matter at all /in practice/ which approach is
taken?

Kind regards

robert

···

2007/9/4, Russell Norris <rsl@swimcommunity.org>:

The problem with that is that || doesn't work that way, imo.

x || whatever

only does whatever if x isn't true. Excuse me if I'm sounding like a
broken record but the more we talk about this the more I'm convinced
that this "bug" only exists if you expect || to act like + does just
because it's an "operator" instead of allowing for the fact that ||
isn't a method like + and friends.

>> is special. It takes another bus to ||= than + does, heh. :wink:

Sorry for the silliness there.

# > If '"x+=1" will always be "x=x+1"', what's the problem in
# having x ||=
# > 1 always be x = x || 1?
# That an assignment of x=x is useless (basically a nop in the standard
# case of x being a variable).

consider,

irb(main):074:0* h=Hash.new
=> {}
irb(main):075:0> h[5] = h[5]
=> nil
irb(main):076:0> h
=> {5=>nil}
irb(main):062:0* h=Hash.new(1)
=> {}
irb(main):063:0> h[5] = h[5]
=> 1
irb(main):064:0> h
=> {5=>1}

thus h[k] = h[k] could mean
    h[k] = h.default or
    h[k] = nil

definitely not useless and not noop.

seeking enlightenment -botp

···

From: Robert Klemme [mailto:shortcutter@googlemail.com]
# 2007/9/4, Yossef Mendelssohn <ymendel@pobox.com>:

I said "noop in the standard case of a variable". We can certainly
debate about usefulness or uselessness. I concede that it's not free
of side effects in the case of a Hash, but the only noticeable side
effect is the change of the key set. Assigning with the Hash's
default is not visible through Hash# although it is with #fetch
which seems to be rarely used from what I see. So I still say that
usefulness of self assignment is very limited.

Kind regards

robert

···

2007/9/5, Peña, Botp <botp@delmonte-phil.com>:

From: Robert Klemme [mailto:shortcutter@googlemail.com]
# 2007/9/4, Yossef Mendelssohn <ymendel@pobox.com>:
# > If '"x+=1" will always be "x=x+1"', what's the problem in
# having x ||=
# > 1 always be x = x || 1?
# That an assignment of x=x is useless (basically a nop in the standard
# case of x being a variable).

consider,

irb(main):074:0* h=Hash.new
=> {}
irb(main):075:0> h[5] = h[5]
=> nil
irb(main):076:0> h
=> {5=>nil}
irb(main):062:0* h=Hash.new(1)
=> {}
irb(main):063:0> h[5] = h[5]
=> 1
irb(main):064:0> h
=> {5=>1}

thus h[k] = h[k] could mean
    h[k] = h.default or
    h[k] = nil

definitely not useless and not noop.

Hi --

···

On Wed, 5 Sep 2007, Peña, Botp wrote:

From: Robert Klemme [mailto:shortcutter@googlemail.com]
# 2007/9/4, Yossef Mendelssohn <ymendel@pobox.com>:
# > If '"x+=1" will always be "x=x+1"', what's the problem in
# having x ||=
# > 1 always be x = x || 1?
# That an assignment of x=x is useless (basically a nop in the standard
# case of x being a variable).

consider,

irb(main):074:0* h=Hash.new
=> {}
irb(main):075:0> h[5] = h[5]
=> nil
irb(main):076:0> h
=> {5=>nil}
irb(main):062:0* h=Hash.new(1)
=> {}
irb(main):063:0> h[5] = h[5]
=> 1
irb(main):064:0> h
=> {5=>1}

thus h[k] = h[k] could mean
   h[k] = h.default or
   h[k] = nil

definitely not useless and not noop.

It's really always h[k] = h.default -- it's just that the default
default, so to speak, is nil.

David

--
* Books:
   RAILS ROUTING (new! http://www.awprofessional.com/title/0321509242\)
   RUBY FOR RAILS (http://www.manning.com/black\)
* Ruby/Rails training
     & consulting: Ruby Power and Light, LLC (http://www.rubypal.com)

Hi --

···

On Wed, 5 Sep 2007, Robert Klemme wrote:

2007/9/5, Peña, Botp <botp@delmonte-phil.com>:

From: Robert Klemme [mailto:shortcutter@googlemail.com]
# 2007/9/4, Yossef Mendelssohn <ymendel@pobox.com>:
# > If '"x+=1" will always be "x=x+1"', what's the problem in
# having x ||=
# > 1 always be x = x || 1?
# That an assignment of x=x is useless (basically a nop in the standard
# case of x being a variable).

consider,

irb(main):074:0* h=Hash.new
=> {}
irb(main):075:0> h[5] = h[5]
=> nil
irb(main):076:0> h
=> {5=>nil}
irb(main):062:0* h=Hash.new(1)
=> {}
irb(main):063:0> h[5] = h[5]
=> 1
irb(main):064:0> h
=> {5=>1}

thus h[k] = h[k] could mean
    h[k] = h.default or
    h[k] = nil

definitely not useless and not noop.

I said "noop in the standard case of a variable". We can certainly
debate about usefulness or uselessness. I concede that it's not free
of side effects in the case of a Hash, but the only noticeable side
effect is the change of the key set. Assigning with the Hash's
default is not visible through Hash# although it is with #fetch
which seems to be rarely used from what I see. So I still say that
usefulness of self assignment is very limited.

I'd be happy for unnecessary assignments to be optimized away, but I
like the conceptual clarity of the x = x || y interpretation for x ||=
y.

David

--
* Books:
   RAILS ROUTING (new! http://www.awprofessional.com/title/0321509242\)
   RUBY FOR RAILS (http://www.manning.com/black\)
* Ruby/Rails training
     & consulting: Ruby Power and Light, LLC (http://www.rubypal.com)