Module: Scruffy::Helpers::PointContainer

Defined in:
lib/scruffy/helpers/point_container.rb

Overview

Scruffy::Helpers::PointContainer

Author

Mat Schaffer

Date

March 22nd, 2007

Allows all standard point operations to be called on both Array and Hash

Defined Under Namespace

Modules: Array_ext, Hash_ext

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(point_set) ⇒ Object



10
11
12
# File 'lib/scruffy/helpers/point_container.rb', line 10

def self.extended point_set
  point_set.extend(const_get("#{point_set.class.to_s}_ext"))
end

Instance Method Details

#inject_with_index(memo) ⇒ Object



34
35
36
37
38
39
40
41
# File 'lib/scruffy/helpers/point_container.rb', line 34

def inject_with_index memo
  index = 0
  inject(memo) do |memo, item|
    ret = yield memo, item, index
    index = index.succ
    ret
  end
end

#maximum_keyObject



47
48
49
# File 'lib/scruffy/helpers/point_container.rb', line 47

def maximum_key
  sortable(keys).sort.last
end

#maximum_valueObject



22
23
24
# File 'lib/scruffy/helpers/point_container.rb', line 22

def maximum_value
  sortable(values).sort.last
end

#minimum_keyObject



43
44
45
# File 'lib/scruffy/helpers/point_container.rb', line 43

def minimum_key
  sortable(keys).sort.first
end

#minimum_valueObject



26
27
28
# File 'lib/scruffy/helpers/point_container.rb', line 26

def minimum_value
  sortable(values).sort.first
end

#sortable(values) ⇒ Object



14
15
16
# File 'lib/scruffy/helpers/point_container.rb', line 14

def sortable(values)
  values.find_all { |v| v.respond_to? :<=> }
end

#sumObject



30
31
32
# File 'lib/scruffy/helpers/point_container.rb', line 30

def sum
  summable(values).inject(0) { |sum, i| sum += i }
end

#summable(values) ⇒ Object



18
19
20
# File 'lib/scruffy/helpers/point_container.rb', line 18

def summable(values)
  values.find_all { |v| v.respond_to? :+ }
end