Class: ArrayWithPriority

Inherits:
Object
  • Object
show all
Defined in:
lib/array_with_priority.rb,
lib/array_with_priority/version.rb

Constant Summary collapse

VERSION =
"0.1.0"

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meffod, *args) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/array_with_priority.rb', line 22

def method_missing meffod, *args
  if ((a = to_a)) && a.respond_to?(meffod)
    if block_given?
      blk = Proc.new
      a.send(meffod, *args, &blk)
    else
      a.send(meffod, *args)
    end
  else
    super
  end
end

Instance Method Details

#<<(obj) ⇒ Object



14
15
16
# File 'lib/array_with_priority.rb', line 14

def << obj
  add obj
end

#add(obj, opts = {}) ⇒ Object



8
9
10
11
12
# File 'lib/array_with_priority.rb', line 8

def add obj, opts={}
  i = opts[:priority] || self.default_priority
  self.array[i] ||= []
  self.array[i] << obj
end

#to_aObject



18
19
20
# File 'lib/array_with_priority.rb', line 18

def to_a
  array.compact
end