Class: Array

Inherits:
Object show all
Defined in:
lib/keystone/core_ext/blank.rb,
lib/keystone/core_ext/array.rb

Overview

:nodoc:

Instance Method Summary collapse

Instance Method Details

#left_join!(name, ar, index1, index2) ⇒ Object

候補が実は複数あるときはエラーを出したいが、、、今のところ無しで



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/keystone/core_ext/array.rb', line 34

def left_join!(name,ar,index1,index2)

  key_hits = []
  has = {}
  ar.each do |o|
    if o.has_key?(index2)
      val = o[index2]
      if key_hits.include?(val)
        raise "right side data is multiple(#{val})"
      end
      h = o.clone
      h.delete(index2)
      has[val] = h
    end
  end

  self.each do |i|
    if has.has_key?(i[index1])
      has[i[index1]].each do |key,value|
        i["#{name.to_s}_#{key}"] = value
      end
    end
  end
  self
end

#sample(count = 1) ⇒ Object



20
21
22
23
24
25
26
27
28
# File 'lib/keystone/core_ext/array.rb', line 20

def sample(count=1)
  if count == 1
    return at( rand( size ) )
  elsif count < 1
    return nil
  else
    return sort_by{rand}[0..count-1]
  end
end

#split_by(num) ⇒ Object

TODO active_supportに同様のメソッドが無いかどうかの確認



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/keystone/core_ext/array.rb', line 7

def split_by(num)
  return [] if self.size < 1
  ret = [[]]
  counter = 0
  self.each do |val|
    (counter = 0;ret << []) if counter >= num
    ret[-1] << val
    counter += 1
  end
  return ret
end