Class: Sequence::OfHash
- Defined in:
- lib/sequence/ofhash.rb
Constant Summary
Constants inherited from Sequence
Instance Attribute Summary
Attributes inherited from Indexed
Instance Method Summary collapse
-
#initialize(hash, exceptions = [], include_default = false, modifiable = false) ⇒ OfHash
constructor
A new instance of OfHash.
- #modify(*args) ⇒ Object
Methods inherited from OfArray
#append, #index, #new_data, #rindex
Methods included from ArrayLike
#data_class, #index, #like, #push, #rindex, #scan, #scan_until, #scanback, #scanback_until, #unshift
Methods inherited from Indexed
_orig_new, #_pos=, #eof?, #maxmatchlen, #maxmatchlen=, new, #new_data
Methods inherited from UseData
#read, #readahead, #readback, #readbehind, #size
Constructor Details
#initialize(hash, exceptions = [], include_default = false, modifiable = false) ⇒ OfHash
Returns a new instance of OfHash.
8 9 10 11 12 13 14 15 |
# File 'lib/sequence/ofhash.rb', line 8 def initialize(hash,exceptions=[],include_default=false,modifiable=false) @hash=hash hash=hash.dup exceptions.each{|exc| hash.delete exc } @data=hash.inject([]){|l,pair| l+pair} @data<<hash.default if include_default @data.freeze unless modifiable end |
Instance Method Details
#modify(*args) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/sequence/ofhash.rb', line 17 def modify(*args) repldata=args.pop start,len,only1=_parse_slice_args(*args) len==1 or raise "scalar modifications to hashes only!" if @data.size.%(2).nonzero? and @data.size.-(1)==start @hash.default=repldata.first elsif start.%(2).zero? #key @hash[repldata.first]=@hash.delete @data[start] else #value @hash[@data[start-1]]=repldata.first end @data[first]=repldata.first repldata end |