Class: Array

Inherits:
Object
  • Object
show all
Defined in:
lib/error.rb,
lib/r-util.rb,
lib/algorithm.rb

Instance Method Summary collapse

Instance Method Details

#check_uniqObject



13
14
15
16
17
18
19
# File 'lib/r-util.rb', line 13

def check_uniq
  hash = {}
  self.each do |x|
    raise "duplicate #{x}" if hash[x]
    hash[x] = true
  end
end

#pcollect(n = nil) ⇒ Object

collect method extended for parallel processing. Note: assign return value as: ans = arr.pcollect(n) { |obj| … }

Parameters:

  • n (defaults to: nil)

    the number of processes to spawn (default: unlimited)



630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
# File 'lib/algorithm.rb', line 630

def pcollect(n = nil)
  nproc = 0
  result = collect do |*a|
    r, w = IO.pipe
    fork do
      r.close
      w.write( Marshal.dump( yield(*a) ) )
    end
    if n and (nproc+=1) >= n
      Process.wait ; nproc -= 1
    end
    [ w.close, r ].last
  end
  Process.waitall
  result.collect{|r| Marshal.load [ r.read, r.close ].first}
end

#short_backtraceObject



91
92
93
94
95
96
97
98
# File 'lib/error.rb', line 91

def short_backtrace
  short = []
  each do |c|
    break if c =~ /sinatra\/base/
    short << c
  end
  short.join("\n")
end