Class: Ramaze::Dictionary
Class Method Summary collapse
-
.[](*args) ⇒ Object
– TODO is this needed? Doesn’t the super class do this? ++.
-
.alpha(*args, &block) ⇒ Object
Alternate to #new which creates a dictionary sorted by key.
-
.auto(*args) ⇒ Object
Alternate to #new which auto-creates sub-dictionaries as needed.
-
.new_by(*args, &blk) ⇒ Object
Like #new but the block sets the order.
Instance Method Summary collapse
- #<<(kv) ⇒ Object
-
#==(hsh2) ⇒ Object
def ==( hsh2 ) return false if @order != hsh2.order super hsh2 end.
- #[](k) ⇒ Object
-
#[]=(k, i = nil, v = nil) ⇒ Object
Store operator.
- #clear ⇒ Object
- #delete(key) ⇒ Object
- #delete_if ⇒ Object
- #dup ⇒ Object
- #each ⇒ Object (also: #each_pair)
- #each_key ⇒ Object
- #each_value ⇒ Object
- #empty? ⇒ Boolean
- #fetch(k) ⇒ Object
- #find ⇒ Object
- #first ⇒ Object
-
#initialize(*args, &blk) ⇒ Dictionary
constructor
A new instance of Dictionary.
- #insert(i, k, v) ⇒ Object
- #inspect ⇒ Object
- #invert ⇒ Object
- #keys ⇒ Object
- #last ⇒ Object
- #length ⇒ Object (also: #size)
- #merge(hsh2) ⇒ Object
- #order ⇒ Object
-
#order_by(&block) ⇒ Object
Keep dictionary sorted by a specific sort order.
-
#order_by_key ⇒ Object
Keep dictionary sorted by key.
-
#order_by_value ⇒ Object
Keep dictionary sorted by value.
- #pop ⇒ Object
- #push(k, v) ⇒ Object
- #reject(&block) ⇒ Object
- #reject!(&block) ⇒ Object
- #reorder ⇒ Object
- #replace(hsh2) ⇒ Object
- #select ⇒ Object
- #shift ⇒ Object
- #store(a, b) ⇒ Object
- #to_a ⇒ Object
- #to_s ⇒ Object
- #unshift(k, v) ⇒ Object
- #update(hsh2) ⇒ Object (also: #merge!)
- #values ⇒ Object
Constructor Details
#initialize(*args, &blk) ⇒ Dictionary
Returns a new instance of Dictionary.
130 131 132 133 134 |
# File 'lib/ramaze/snippets/dictionary.rb', line 130 def initialize( *args, &blk ) @order = [] @order_by = nil @hash = Hash.new( *args, &blk ) end |
Class Method Details
.[](*args) ⇒ Object
– TODO is this needed? Doesn’t the super class do this? ++
82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/ramaze/snippets/dictionary.rb', line 82 def []( *args ) hsh = new if Hash === args[0] hsh.replace(args[0]) elsif (args.size % 2) != 0 raise ArgumentError, "odd number of elements for Hash" else while !args.empty? hsh[args.shift] = args.shift end end hsh end |
.alpha(*args, &block) ⇒ Object
Alternate to #new which creates a dictionary sorted by key.
d = Dictionary.alpha
d["z"] = 1
d["y"] = 2
d["x"] = 3
d #=> {"x"=>3,"y"=>2,"z"=>2}
This is equivalent to:
Dictionary.new.order_by { |key,value| key }
114 115 116 |
# File 'lib/ramaze/snippets/dictionary.rb', line 114 def alpha( *args, &block ) new( *args, &block ).order_by_key end |
.auto(*args) ⇒ Object
Alternate to #new which auto-creates sub-dictionaries as needed.
d = Dictionary.auto
d["a"]["b"]["c"] = "abc" #=> { "a"=>{"b"=>{"c"=>"abc"}}}
123 124 125 126 127 |
# File 'lib/ramaze/snippets/dictionary.rb', line 123 def auto( *args ) #AutoDictionary.new(*args) leet = lambda { |hsh, key| hsh[key] = new( &leet ) } new(*args, &leet) end |
.new_by(*args, &blk) ⇒ Object
Like #new but the block sets the order.
98 99 100 |
# File 'lib/ramaze/snippets/dictionary.rb', line 98 def new_by( *args, &blk ) new(*args).order_by(&blk) end |
Instance Method Details
#<<(kv) ⇒ Object
321 322 323 |
# File 'lib/ramaze/snippets/dictionary.rb', line 321 def <<(kv) push(*kv) end |
#==(hsh2) ⇒ Object
def ==( hsh2 )
return false if @order != hsh2.order
super hsh2
end
202 203 204 205 206 207 208 209 |
# File 'lib/ramaze/snippets/dictionary.rb', line 202 def ==( hsh2 ) if hsh2.is_a?( Dictionary ) @order == hsh2.order && @hash == hsh2.instance_variable_get("@hash") else false end end |
#[](k) ⇒ Object
211 212 213 |
# File 'lib/ramaze/snippets/dictionary.rb', line 211 def [] k @hash[ k ] end |
#[]=(k, i = nil, v = nil) ⇒ Object
Store operator.
h[key] = value
Or with additional index.
h[key,index] = value
227 228 229 230 231 232 233 |
# File 'lib/ramaze/snippets/dictionary.rb', line 227 def []=(k, i=nil, v=nil) if v insert(i,k,v) else store(k,i) end end |
#clear ⇒ Object
245 246 247 248 |
# File 'lib/ramaze/snippets/dictionary.rb', line 245 def clear @order = [] @hash.clear end |
#delete(key) ⇒ Object
250 251 252 253 |
# File 'lib/ramaze/snippets/dictionary.rb', line 250 def delete( key ) @order.delete( key ) @hash.delete( key ) end |
#delete_if ⇒ Object
271 272 273 274 |
# File 'lib/ramaze/snippets/dictionary.rb', line 271 def delete_if order.clone.each { |k| delete k if yield } self end |
#dup ⇒ Object
356 357 358 |
# File 'lib/ramaze/snippets/dictionary.rb', line 356 def dup self.class[*to_a.flatten] end |
#each ⇒ Object Also known as: each_pair
265 266 267 268 |
# File 'lib/ramaze/snippets/dictionary.rb', line 265 def each order.each { |k| yield( k,@hash[k] ) } self end |
#each_key ⇒ Object
255 256 257 258 |
# File 'lib/ramaze/snippets/dictionary.rb', line 255 def each_key order.each { |k| yield( k ) } self end |
#each_value ⇒ Object
260 261 262 263 |
# File 'lib/ramaze/snippets/dictionary.rb', line 260 def each_value order.each { |k| yield( @hash[k] ) } self end |
#empty? ⇒ Boolean
395 396 397 |
# File 'lib/ramaze/snippets/dictionary.rb', line 395 def empty? @hash.empty? end |
#fetch(k) ⇒ Object
215 216 217 |
# File 'lib/ramaze/snippets/dictionary.rb', line 215 def fetch( k ) @hash.fetch( k ) end |
#find ⇒ Object
377 378 379 380 |
# File 'lib/ramaze/snippets/dictionary.rb', line 377 def find each{|k,v| return k, v if yield(k,v) } return nil end |
#first ⇒ Object
382 383 384 |
# File 'lib/ramaze/snippets/dictionary.rb', line 382 def first @hash[order.first] end |
#insert(i, k, v) ⇒ Object
235 236 237 238 |
# File 'lib/ramaze/snippets/dictionary.rb', line 235 def insert( i,k,v ) @order.insert( i,k ) @hash.store( k,v ) end |
#inspect ⇒ Object
350 351 352 353 354 |
# File 'lib/ramaze/snippets/dictionary.rb', line 350 def inspect ary = [] each {|k,v| ary << k.inspect + "=>" + v.inspect} '{' + ary.join(", ") + '}' end |
#invert ⇒ Object
286 287 288 289 290 |
# File 'lib/ramaze/snippets/dictionary.rb', line 286 def invert hsh2 = self.class.new order.each { |k| hsh2[@hash[k]] = k } hsh2 end |
#keys ⇒ Object
282 283 284 |
# File 'lib/ramaze/snippets/dictionary.rb', line 282 def keys order end |
#last ⇒ Object
386 387 388 |
# File 'lib/ramaze/snippets/dictionary.rb', line 386 def last @hash[order.last] end |
#length ⇒ Object Also known as: size
390 391 392 |
# File 'lib/ramaze/snippets/dictionary.rb', line 390 def length @order.length end |
#merge(hsh2) ⇒ Object
367 368 369 |
# File 'lib/ramaze/snippets/dictionary.rb', line 367 def merge( hsh2 ) self.dup.update(hsh2) end |
#order ⇒ Object
136 137 138 139 |
# File 'lib/ramaze/snippets/dictionary.rb', line 136 def order reorder if @order_by @order end |
#order_by(&block) ⇒ Object
Keep dictionary sorted by a specific sort order.
143 144 145 146 147 |
# File 'lib/ramaze/snippets/dictionary.rb', line 143 def order_by( &block ) @order_by = block order self end |
#order_by_key ⇒ Object
Keep dictionary sorted by key.
d = Dictionary.new.order_by_key
d["z"] = 1
d["y"] = 2
d["x"] = 3
d #=> {"x"=>3,"y"=>2,"z"=>2}
This is equivalent to:
Dictionary.new.order_by { |key,value| key }
The initializer Dictionary#alpha also provides this.
163 164 165 166 167 |
# File 'lib/ramaze/snippets/dictionary.rb', line 163 def order_by_key @order_by = lambda { |k,v| k } order self end |
#order_by_value ⇒ Object
Keep dictionary sorted by value.
d = Dictionary.new.order_by_value
d["z"] = 1
d["y"] = 2
d["x"] = 3
d #=> {"x"=>3,"y"=>2,"z"=>2}
This is equivalent to:
Dictionary.new.order_by { |key,value| value }
181 182 183 184 185 |
# File 'lib/ramaze/snippets/dictionary.rb', line 181 def order_by_value @order_by = lambda { |k,v| v } order self end |
#pop ⇒ Object
335 336 337 338 |
# File 'lib/ramaze/snippets/dictionary.rb', line 335 def pop key = order.last key ? [key,delete(key)] : nil end |
#push(k, v) ⇒ Object
325 326 327 328 329 330 331 332 333 |
# File 'lib/ramaze/snippets/dictionary.rb', line 325 def push( k,v ) unless @hash.include?( k ) @order.push( k ) @hash.store( k,v ) true else false end end |
#reject(&block) ⇒ Object
292 293 294 |
# File 'lib/ramaze/snippets/dictionary.rb', line 292 def reject( &block ) self.dup.delete_if(&block) end |
#reject!(&block) ⇒ Object
296 297 298 299 |
# File 'lib/ramaze/snippets/dictionary.rb', line 296 def reject!( &block ) hsh2 = reject(&block) self == hsh2 ? nil : hsh2 end |
#reorder ⇒ Object
189 190 191 192 193 194 195 |
# File 'lib/ramaze/snippets/dictionary.rb', line 189 def reorder if @order_by assoc = @order.collect{ |k| [k,@hash[k]] }.sort_by( &@order_by ) @order = assoc.collect{ |k,v| k } end @order end |
#replace(hsh2) ⇒ Object
301 302 303 304 |
# File 'lib/ramaze/snippets/dictionary.rb', line 301 def replace( hsh2 ) @order = hsh2.order @hash = hsh2.hash end |
#select ⇒ Object
371 372 373 374 375 |
# File 'lib/ramaze/snippets/dictionary.rb', line 371 def select ary = [] each { |k,v| ary << [k,v] if yield k,v } ary end |
#shift ⇒ Object
306 307 308 309 |
# File 'lib/ramaze/snippets/dictionary.rb', line 306 def shift key = order.first key ? [key,delete(key)] : super end |
#store(a, b) ⇒ Object
240 241 242 243 |
# File 'lib/ramaze/snippets/dictionary.rb', line 240 def store( a,b ) @order.push( a ) unless @hash.has_key?( a ) @hash.store( a,b ) end |
#to_a ⇒ Object
340 341 342 343 344 |
# File 'lib/ramaze/snippets/dictionary.rb', line 340 def to_a ary = [] each { |k,v| ary << [k,v] } ary end |
#to_s ⇒ Object
346 347 348 |
# File 'lib/ramaze/snippets/dictionary.rb', line 346 def to_s self.to_a.to_s end |
#unshift(k, v) ⇒ Object
311 312 313 314 315 316 317 318 319 |
# File 'lib/ramaze/snippets/dictionary.rb', line 311 def unshift( k,v ) unless @hash.include?( k ) @order.unshift( k ) @hash.store( k,v ) true else false end end |
#update(hsh2) ⇒ Object Also known as: merge!
360 361 362 363 364 |
# File 'lib/ramaze/snippets/dictionary.rb', line 360 def update( hsh2 ) hsh2.each { |k,v| self[k] = v } reorder self end |
#values ⇒ Object
276 277 278 279 280 |
# File 'lib/ramaze/snippets/dictionary.rb', line 276 def values ary = [] order.each { |k| ary.push @hash[k] } ary end |