Topic: efficient hash manipulation?

How can i mask a key value in a hash efficiently without modifying the orignal hash and producing a new hash?

Re: efficient hash manipulation?

I'm not sure how it would be possible to do this without modifying the original hash or creating a new one. Can you be more descriptive in your question?

Railscasts - Free Ruby on Rails Screencasts

Re: efficient hash manipulation?

I meant how could i do it succintly

Re: efficient hash manipulation?

Are you just trying to remove a certain key/value from a hash? If so then there's this.

h.delete(:foo)

If not, sorry I don't understand.

Railscasts - Free Ruby on Rails Screencasts

Re: efficient hash manipulation?

i meant masking. For example the value for password key should be replaced by 'XXXXXXX'

Re: efficient hash manipulation?

h[:password] = 'xxxx'

Or maybe I'm still not getting it?

Railscasts - Free Ruby on Rails Screencasts

Re: efficient hash manipulation?

I was thinking something liek this:
h = {}
org.each { |key,val| } do
   if val.include? "PWD"
       h[:key] = 'xxxx'
   else
       h[:key] = val
   end
end

Last edited by RubySmith (2007-05-24 22:54:44)

Re: efficient hash manipulation?

Insertion into a hashtable already happens in constant time so insertion + removal, both O(1), is the fastest way to do it.