class Array
def to_hash; Hash[*map {|x| [x, yield(x)] }.flatten]; end
end
(Edit) Voici une version plus détaillée:
class Array
def to_hash
keys_and_values = self.map {|x| [x, yield(x)] }
# Now `keys_and_values` is an array of arrays reperesenting
# the hash. If the array this method was called on was, for
# example, `[1, 2, 3]`, and the block passed to this method
# was `{|x| x + 1 }`, `keys_and_values` would be:
#
# [[1, 2], [2, 3], [3, 4]]
keys_and_values = keys_and_values.flatten
# now `keys_and_values` still contains all of the keys/values
# of the new hash, but without the inner arrays. Even numbered
# indexes will be keys, and odd indexes will be values. Example:
#
# [1, 2, 2, 3, 3, 4]
Hash[*keys_and_values]
# This returns the keys/values translated to a hash. The docs
# for the `Hash.[]` method is here:
#
# http://ruby-doc.org/core/classes/Hash.html#M002839
end
end
est pas la définition même de subjectif. devrait au moins être CW – shsteimer