ara.t.howard / noaa.gov wrote:
> On Thu, 18 Jan 2007, Trans wrote:
>
> > hi--
> >
> > have a look:
> >
> >  class Hash
> >
> >    def open!
> >      class << self
> >        @_were_public = public_instance_methods - ['close!']
> >        @_were_public.each { |m| private m }
> >        def method_missing(s,*a)
> >          if s.to_s[-1,1] == '='
> >            self[s] = a.first
> >          else
> >            return self[s]
> >          end
> >        end
> >      end
> >    end
> >
> >    def close!
> >      class << self
> >        @_were_public.each { |m| public m }
> >        @_were_public = nil
> >        remove_method(:method_missing)
> >      end
> >    end
> >
> >  end
> >
> > usage:
> >
> >  h = {a=>1, :sort_by=>2}
> >  h.a  #=> NoMethodError
> >  h.sort_by #=> LocalJumpError
> >  h.open!
> >  h.a  #=> 1
> >  h.sort_by #=> 2
> >  h.close!
> >  h.a  #=> NoMethodError
> >  h.sort_by #=> LocalJumpError
> >
> > thoughts? improvements? useful? bad-news?
> >
> > T
>
> the only real disadvantage i see is that all these
>
>      find_all
>      keys
>      []=
>      each
>      object_id
>      singleton_methods
>      inject
>      delete
>      value?
>      to_hash
>      equal?
>      taint
>      sort_by
>      frozen?
>      instance_variable_get
>      max
>      kind_of?
>      each_pair
>      respond_to?
>      to_a
>      delete_if
>      merge!
>      index
>      select
>      merge
>      length
>      type
>      partition
>      protected_methods
>      store
>      grep
>      eql?
>      instance_variable_set
>      hash
>      is_a?
>      values
>      reject
>      to_s
>      send
>      default
>      class
>      size
>      tainted?
>      private_methods
>      __send__
>      member?
>      default=
>      default_proc
>      untaint
>      find
>      each_with_index
>      reject!
>      id
>      invert
>      instance_eval
>      collect
>      inspect
>      has_key?
>      replace
>      all?
>      ==
>      ===
>      indexes
>      entries
>      clone
>      public_methods
>      extend
>      each_value
>      fetch
>      detect
>      freeze
>      values_at
>      zip
>      display
>      update
>      __id__
>      shift
>      method
>      has_value?
>      empty?
>      map
>      =~
>      methods
>      clear
>      any?
>      rehash
>      nil?
>      sort
>      dup
>      indices
>      key?
>      min
>      instance_variables
>      include?
>      []
>      instance_of?
>      each_key
>
>
> cannot be keys since method_missing is use to set the key.  an impl like this
> could get around that

actually they are made private in my implementation (well, ones with
only alphanumeric characters) and that allows method_missing to get at
them -- yea, one of those esoteric peices of knowledge about ruby one
doesn't readily remember even when it is known.

T.