On Thu, May 1, 2008 at 3:07 AM, Eric Hodel <drbrain / segment7.net> wrote:
> On Apr 30, 2008, at 14:00 PM, Joe K wrote:
> > Consider the common case of something like DataStructure#remove(). Does
> > remove take an index or the object to be removed? Usually the API
> developer
> > supports one use case or another but not both. You should not have to read
> > the code to figure out which it is.
> >
>
>  That API developer should be beaten with a stick.  Matz programmed my brain
> to expect this as the way to remove elements from a data structure by
> object:
>
>  $ ri Hash#delete
>  ------------------------------------------------------------ Hash#delete
>      hsh.delete(key)                   => value
>  $ ri Array#delete
>  ----------------------------------------------------------- Array#delete
>      array.delete(obj)            -> obj or nil
>
>  And this as the way to remove elements from a data structure by index:
>
>  $ ri Array#delete_at
>  -------------------------------------------------------- Array#delete_at
>      array.delete_at(index)  -> obj or nil


Truth be told, I think that this actually is evidence that delete
sometimes deletes by object, and sometimes by index, at least if you
think of keys as Hash indices

There are some other differences if you look at the full doc,
Array#delete will delete multiple occurences of the value, and will
return nil if there is no occurrence.  Hash#delete can only delete one
think since it's deleting by key and keys are unique, and Hash#delete
returns the default value for the hash if the key is not found.  There
seems to be a doc bug in Hash#delete since it says that it returns the
key value pair, but it seems to return just the value.

If Hash#delete and Array#delete were really parallel then Hash#delete
would be something like:

class Hash
  def delete(object)
     default = nil if default == object
     if has_value?(object)
       delete_if {|key, value| value == object}
       object
    else
       nil
    end
  end
end

Not that I'd argue for this change.

-- 
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/