by masklinn · 13y ago · 0 comments
That's not a good reason for anything, by that yardstick Array#length could return a float and nil#nil? could return an array. And worse, the `?` postfix in Ruby normally means the method returns an actual boolean, not a "boolean context" (convention over configuration only works if the convention is respected). You can't XOR a string and a boolean (no `^` defined on string, although XOR-ing a boolean and a string will work), you can XOR two booleans.
> Why not? It is possible to shoot yourself in the leg, but otherwise a very useful feature.
I'm not sure, but I think he phrased it badly and asks why "interpreter-provided" mutables (e.g. Array) can be set as hash key: while "user objects" are also hashable by default in Python, built-in collections and the like are specified as unhashable as the semantics are not really sensible:
>>> {{}: 1}
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'dict'
>>> {set(): 1}
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'set'
versus irb(main):002:0> {[] => 1}
=> {[]=>1}
irb(main):003:0> {{} => 1}
=> {{}=>1}That's not true. Predicates methods returns Truthy or Falsey values. Not necessarily booleans. See http://blog.leshill.org/blog/2012/03/25/a-question-of-truth....
Original intent aside, if your language is prided on convention, then it has to be open to the convention changing.
Btw: The mantra "convention over configuration" is a rails mantra and not a ruby mantra.
Also, I never said "convention over configuration," since there's nothing to configure here. I was talking specifically about the convention of what a "?" should return. In that same quote Matz also says that predicates typically return a boolean value, but it's not required. That seems to both imply and endorse a convention.
While the actual return value can be useful, it would be just as useful if the method had a more sensible name than hinting it's a predicate, which it is not.
(i agree that within ruby's conventions (weak typing) defined? is not that bad)
data[[v1, v2]] = something
and because strings are mutable in ruby.If you couldn't use mutable objects as hash keys a bunch of things would become _way_ harder.
data[[v1, v2].freeze] = something
meh. Hell, Hash could even freeze its parameters on entry if they're not already frozen.Meanwhile if anybody gets a hold of the key and happens to mutate it (not necessarily for nefarious purposes, just because it solves they problem) you can quite literally lose your pair:
irb(main):001:0> h = {}
=> {}
irb(main):002:0> l = [1, 2]
=> [1, 2]
irb(main):003:0> h[l] = 1
=> 1
irb(main):004:0> h[l]
=> 1
irb(main):005:0> l[1] = 3
=> 3
irb(main):006:0> h[l]
=> nil
irb(main):007:0> h[[1, 2]]
=> nil
irb(main):009:0> h[[1, 3]]
=> nil
irb(main):010:0> h
=> {[1, 3]=>1}
irb(main):011:0> h[[1, 3]] = 3
=> 3
irb(main):012:0> h
=> {[1, 3]=>1, [1, 3]=>3} jruby-1.6.8 :068 > h = {}
=> {}
jruby-1.6.8 :069 > l = [1, 2]
=> [1, 2]
jruby-1.6.8 :071 > h[l] = 1
=> 1
jruby-1.6.8 :072 > h[l]
=> 1
jruby-1.6.8 :073 > h[l] = 3
=> 3
jruby-1.6.8 :074 > h[l]
=> 3
jruby-1.6.8 :075 > l[1] = 3
=> 3
jruby-1.6.8 :076 > h[l]
=> nil
jruby-1.6.8 :077 > h.rehash
=> {[1, 3]=>3}
jruby-1.6.8 :078 > h[l]
=> 3
see http://www.ruby-doc.org/core-1.9.3/Hash.html#method-i-rehash : Rebuilds the hash based on the current hash values for each key. If values
of key objects have changed since they were inserted, this method will
reindex hsh.
On one side I must admit that the behavior is a bit surprising I haven't ever encountered in the wild. It's not that easy to get hold of a hash key at a random point in the code where you don't know that it is a hash key.