Class: Sequence::OfHash

Inherits:
OfArray show all
Defined in:
lib/sequence/ofhash.rb

Constant Summary

Constants inherited from Sequence

SubSequence, VERSION

Instance Attribute Summary

Attributes inherited from Indexed

#data, #pos

Attributes inherited from Sequence

#last_match, #maxmatchlen

Instance Method Summary collapse

Methods inherited from OfArray

#append, #index, #new_data, #rindex

Methods included from ArrayLike

#data_class, #index, #like, #new_data, #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

Methods inherited from Sequence

#+, #-, #<<, [], #[], #[]=, #_adjust_pos_on_change, #_default_maxmatchlen, #_delete_position, #_normalize_pos, #_parse_slice_args, #_pos=, #all_data, #append, #begin, #begin!, #check, #check_until, #checkback, #checkback_until, #close, #closed?, #delete, #each, #empty?, #end, #end!, #eof?, #exist?, #existback?, #first, #goto, #holding, #holding!, #holding?, #holding_position, #holding_position!, #holding_position?, #insert, #last, #length, #match?, #matchback?, #more_data?, #move, #move!, #nearbegin, #nearend, #new, #notify_change, #on_change_notify, #original__new, #overwrite, #pop, #pos, #pos=, #pos?, #position, #position=, #position?, #pred, #prepend, #prop, #read, #read!, #read1, #readahead, #readahead1, #readback, #readback1, #readbehind, #readbehind1, #rest_size, #reversed, #shift, #size, #skip, #skip_literal, #skip_until, #skip_until_literal, #skipback, #skipback_until, #slice, #slice!, #slice1, #slice1!, #subseq, #succ, #to_sequence, #was_data?, #write, #writeahead, #writeback, #writebehind

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