Parent

Class Index [+]

Quicksearch

Ramaze::Dictionary

Public Class Methods

[]( *args ) click to toggle source
    # File lib/ramaze/snippets/ramaze/dictionary.rb, line 82
82:       def []( *args )
83:         hsh = new
84:         if Hash === args[0]
85:           hsh.replace(args[0])
86:         elsif (args.size % 2) != 0
87:           raise ArgumentError, "odd number of elements for Hash"
88:         else
89:           while !args.empty?
90:             hsh[args.shift] = args.shift
91:           end
92:         end
93:         hsh
94:       end
alpha( *args, &block ) click to toggle source

Alternate to # 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 }
     # File lib/ramaze/snippets/ramaze/dictionary.rb, line 114
114:       def alpha( *args, &block )
115:         new( *args, &block ).order_by_key
116:       end
auto( *args ) click to toggle source

Alternate to # which auto-creates sub-dictionaries as needed.

  d = Dictionary.auto
  d["a"]["b"]["c"] = "abc"  #=> { "a"=>{"b"=>{"c"=>"abc"}}}
     # File lib/ramaze/snippets/ramaze/dictionary.rb, line 123
123:       def auto( *args )
124:         #AutoDictionary.new(*args)
125:         leet = lambda { |hsh, key| hsh[key] = new( &leet ) }
126:         new(*args, &leet)
127:       end
new( *args, &blk ) click to toggle source
     # File lib/ramaze/snippets/ramaze/dictionary.rb, line 130
130:     def initialize( *args, &blk )
131:       @order = []
132:       @order_by = nil
133:       @hash = Hash.new( *args, &blk )
134:     end
new_by( *args, &blk ) click to toggle source

Like # but the block sets the order.

     # File lib/ramaze/snippets/ramaze/dictionary.rb, line 98
 98:       def new_by( *args, &blk )
 99:         new(*args).order_by(&blk)
100:       end

Public Instance Methods

<<(kv) click to toggle source
     # File lib/ramaze/snippets/ramaze/dictionary.rb, line 321
321:     def <<(kv)
322:       push(*kv)
323:     end
==( hsh2 ) click to toggle source

def ==( hsh2 )

  return false if @order != hsh2.order
  super hsh2

end

     # File lib/ramaze/snippets/ramaze/dictionary.rb, line 202
202:     def ==( hsh2 )
203:       if hsh2.is_a?( Dictionary )
204:         @order == hsh2.order &&
205:           @hash  == hsh2.instance_variable_get("@hash")
206:       else
207:         false
208:       end
209:     end
[](k) click to toggle source
     # File lib/ramaze/snippets/ramaze/dictionary.rb, line 211
211:     def [] k
212:       @hash[ k ]
213:     end
[]=(k, i=nil, v=nil) click to toggle source

Store operator.

  h[key] = value

Or with additional index.

 h[key,index] = value
     # File lib/ramaze/snippets/ramaze/dictionary.rb, line 227
227:     def []=(k, i=nil, v=nil)
228:       if v
229:         insert(i,k,v)
230:       else
231:         store(k,i)
232:       end
233:     end
clear() click to toggle source
     # File lib/ramaze/snippets/ramaze/dictionary.rb, line 245
245:     def clear
246:       @order = []
247:       @hash.clear
248:     end
delete( key ) click to toggle source
     # File lib/ramaze/snippets/ramaze/dictionary.rb, line 250
250:     def delete( key )
251:       @order.delete( key )
252:       @hash.delete( key )
253:     end
delete_if() click to toggle source
     # File lib/ramaze/snippets/ramaze/dictionary.rb, line 271
271:     def delete_if
272:       order.clone.each { |k| delete k if yield }
273:       self
274:     end
dup() click to toggle source
     # File lib/ramaze/snippets/ramaze/dictionary.rb, line 356
356:     def dup
357:       self.class[*to_a.flatten]
358:     end
each() click to toggle source
     # File lib/ramaze/snippets/ramaze/dictionary.rb, line 265
265:     def each
266:       order.each { |k| yield( k,@hash[k] ) }
267:       self
268:     end
Also aliased as: each_pair
each_key() click to toggle source
     # File lib/ramaze/snippets/ramaze/dictionary.rb, line 255
255:     def each_key
256:       order.each { |k| yield( k ) }
257:       self
258:     end
each_pair() click to toggle source
Alias for: each
each_value() click to toggle source
     # File lib/ramaze/snippets/ramaze/dictionary.rb, line 260
260:     def each_value
261:       order.each { |k| yield( @hash[k] ) }
262:       self
263:     end
empty?() click to toggle source
     # File lib/ramaze/snippets/ramaze/dictionary.rb, line 395
395:     def empty?
396:       @hash.empty?
397:     end
fetch( k ) click to toggle source
     # File lib/ramaze/snippets/ramaze/dictionary.rb, line 215
215:     def fetch( k )
216:       @hash.fetch( k )
217:     end
find() click to toggle source
     # File lib/ramaze/snippets/ramaze/dictionary.rb, line 377
377:     def find
378:       each{|k,v| return k, v if yield(k,v) }
379:       return nil
380:     end
first() click to toggle source
     # File lib/ramaze/snippets/ramaze/dictionary.rb, line 382
382:     def first
383:       @hash[order.first]
384:     end
insert( i,k,v ) click to toggle source
     # File lib/ramaze/snippets/ramaze/dictionary.rb, line 235
235:     def insert( i,k,v )
236:       @order.insert( i,k )
237:       @hash.store( k,v )
238:     end
inspect() click to toggle source
     # File lib/ramaze/snippets/ramaze/dictionary.rb, line 350
350:     def inspect
351:       ary = []
352:       each {|k,v| ary << k.inspect + "=>" + v.inspect}
353:       '{' + ary.join(", ") + '}'
354:     end
invert() click to toggle source
     # File lib/ramaze/snippets/ramaze/dictionary.rb, line 286
286:     def invert
287:       hsh2 = self.class.new
288:       order.each { |k| hsh2[@hash[k]] = k }
289:       hsh2
290:     end
keys() click to toggle source
     # File lib/ramaze/snippets/ramaze/dictionary.rb, line 282
282:     def keys
283:       order
284:     end
last() click to toggle source
     # File lib/ramaze/snippets/ramaze/dictionary.rb, line 386
386:     def last
387:       @hash[order.last]
388:     end
length() click to toggle source
     # File lib/ramaze/snippets/ramaze/dictionary.rb, line 390
390:     def length
391:       @order.length
392:     end
Also aliased as: size
merge( hsh2 ) click to toggle source
     # File lib/ramaze/snippets/ramaze/dictionary.rb, line 367
367:     def merge( hsh2 )
368:       self.dup.update(hsh2)
369:     end
merge!( hsh2 ) click to toggle source
Alias for: update
order() click to toggle source
     # File lib/ramaze/snippets/ramaze/dictionary.rb, line 136
136:     def order
137:       reorder if @order_by
138:       @order
139:     end
order_by( &block ) click to toggle source

Keep dictionary sorted by a specific sort order.

     # File lib/ramaze/snippets/ramaze/dictionary.rb, line 143
143:     def order_by( &block )
144:       @order_by = block
145:       order
146:       self
147:     end
order_by_key() click to toggle source

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.

     # File lib/ramaze/snippets/ramaze/dictionary.rb, line 163
163:     def order_by_key
164:       @order_by = lambda { |k,v| k }
165:       order
166:       self
167:     end
order_by_value() click to toggle source

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 }
     # File lib/ramaze/snippets/ramaze/dictionary.rb, line 181
181:     def order_by_value
182:       @order_by = lambda { |k,v| v }
183:       order
184:       self
185:     end
pop() click to toggle source
     # File lib/ramaze/snippets/ramaze/dictionary.rb, line 335
335:     def pop
336:       key = order.last
337:       key ? [key,delete(key)] : nil
338:     end
push( k,v ) click to toggle source
     # File lib/ramaze/snippets/ramaze/dictionary.rb, line 325
325:     def push( k,v )
326:       unless @hash.include?( k )
327:         @order.push( k )
328:         @hash.store( k,v )
329:         true
330:       else
331:         false
332:       end
333:     end
reject( &block ) click to toggle source
     # File lib/ramaze/snippets/ramaze/dictionary.rb, line 292
292:     def reject( &block )
293:       self.dup.delete_if(&block)
294:     end
reject!( &block ) click to toggle source
     # File lib/ramaze/snippets/ramaze/dictionary.rb, line 296
296:     def reject!( &block )
297:       hsh2 = reject(&block)
298:       self == hsh2 ? nil : hsh2
299:     end
reorder() click to toggle source
     # File lib/ramaze/snippets/ramaze/dictionary.rb, line 189
189:     def reorder
190:       if @order_by
191:         assoc = @order.collect{ |k| [k,@hash[k]] }.sort_by( &@order_by )
192:         @order = assoc.collect{ |k,v| k }
193:       end
194:       @order
195:     end
replace( hsh2 ) click to toggle source
     # File lib/ramaze/snippets/ramaze/dictionary.rb, line 301
301:     def replace( hsh2 )
302:       @order = hsh2.order
303:       @hash = hsh2.hash
304:     end
select() click to toggle source
     # File lib/ramaze/snippets/ramaze/dictionary.rb, line 371
371:     def select
372:       ary = []
373:       each { |k,v| ary << [k,v] if yield k,v }
374:       ary
375:     end
shift() click to toggle source
     # File lib/ramaze/snippets/ramaze/dictionary.rb, line 306
306:     def shift
307:       key = order.first
308:       key ? [key,delete(key)] : super
309:     end
size() click to toggle source
Alias for: length
store( a,b ) click to toggle source
     # File lib/ramaze/snippets/ramaze/dictionary.rb, line 240
240:     def store( a,b )
241:       @order.push( a ) unless @hash.has_key?( a )
242:       @hash.store( a,b )
243:     end
to_a() click to toggle source
     # File lib/ramaze/snippets/ramaze/dictionary.rb, line 340
340:     def to_a
341:       ary = []
342:       each { |k,v| ary << [k,v] }
343:       ary
344:     end
to_s() click to toggle source
     # File lib/ramaze/snippets/ramaze/dictionary.rb, line 346
346:     def to_s
347:       self.to_a.to_s
348:     end
unshift( k,v ) click to toggle source
     # File lib/ramaze/snippets/ramaze/dictionary.rb, line 311
311:     def unshift( k,v )
312:       unless @hash.include?( k )
313:         @order.unshift( k )
314:         @hash.store( k,v )
315:         true
316:       else
317:         false
318:       end
319:     end
update( hsh2 ) click to toggle source
     # File lib/ramaze/snippets/ramaze/dictionary.rb, line 360
360:     def update( hsh2 )
361:       hsh2.each { |k,v| self[k] = v }
362:       reorder
363:       self
364:     end
Also aliased as: merge!
values() click to toggle source
     # File lib/ramaze/snippets/ramaze/dictionary.rb, line 276
276:     def values
277:       ary = []
278:       order.each { |k| ary.push @hash[k] }
279:       ary
280:     end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.