Class: Array

Inherits:
Object show all
Defined in:
lib/poolparty/core/array.rb

Direct Known Subclasses

Aska::Rules

Instance Method Summary collapse

Instance Method Details

#collect_with_index(&block) ⇒ Object



22
23
24
# File 'lib/poolparty/core/array.rb', line 22

def collect_with_index &block
  self.enum_for(:each_with_index).collect &block
end

#get_named(str = "") ⇒ Object



38
39
40
# File 'lib/poolparty/core/array.rb', line 38

def get_named(str="")
  map {|a| a.name == str ? a : nil }.reject {|a| a.nil? }
end

#histogramObject



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/poolparty/core/array.rb', line 10

def histogram
  out = {}
  map do |ele|
    if out.has_key?(ele)
      out[ele] += 1
    else
      out.merge!(ele => 1)
    end
  end
  out
end

#nice_runnable(quiet = true) ⇒ Object



30
31
32
# File 'lib/poolparty/core/array.rb', line 30

def nice_runnable(quiet=true)
  self.flatten.reject{|e| (e.nil? || e.empty?) }.join(" \n ").chomp.nice_runnable(quiet)
end

#respec_string(ns = []) ⇒ Object



42
43
44
# File 'lib/poolparty/core/array.rb', line 42

def respec_string(ns=[])
  "'#{map {|e| e.to_option_string }.join("', '")}'"
end

#runnable(quiet = true) ⇒ Object



26
27
28
# File 'lib/poolparty/core/array.rb', line 26

def runnable(quiet=true)
  self.join(" \n ").runnable(quiet)
end

#select_with_hash(conditions = {}) ⇒ Object

Example nodes.select_with_hash(:status=>‘running’)



46
47
48
49
50
51
52
53
# File 'lib/poolparty/core/array.rb', line 46

def select_with_hash(conditions={})
  return self if conditions.empty?
  select do |node|
    conditions.any? do |k,v|
      ( node.has_key?(k) && node[k]==v ) or ( node.respond_to?(k) && node.send(k)==v )
    end
  end
end

#to_osObject



6
7
8
# File 'lib/poolparty/core/array.rb', line 6

def to_os
  map {|a| a.to_os }
end

#to_string(pre = "") ⇒ Object



34
35
36
# File 'lib/poolparty/core/array.rb', line 34

def to_string(pre="")
  map {|a| a.to_string(pre)}.join("\n")
end

#wrapping_next(id) ⇒ Object



55
56
57
58
# File 'lib/poolparty/core/array.rb', line 55

def wrapping_next(id)
  raise "Element #{id} not in array" unless index(id)
  index(id) >= size-1 ? at(0) : at(index(id)+1)
end