Method: LEON.pluck

Defined in:
lib/leon.rb

.pluck(arr, prop) ⇒ Object



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/leon.rb', line 131

def self.pluck(arr, prop)
  ret = Array.new
  if prop.kind_of? Symbol
    prop = prop.to_s
  end
  for i in 0...(arr.length)
    if arr[i].kind_of? Hash
      if not arr[i].has_key? prop
        if not arr[i].has_key? prop.to_sym
          raise "Object #{i} in array has no such key \"#{prop}.\""
        else
          ret.push arr[i][prop.to_sym]
        end
      else
        ret.push arr[i][prop]
      end
    elsif arr[i].kind_of? Object
      begin
        ret.push arr[i].send(prop)
      rescue
        raise "Object #{i} in array has no such property \"#{prop}.\""
      end
    end
  end
  return ret
end