Томислав обнови решението на 24.10.2011 11:29 (преди около 13 години)
+class Array
+
+ def to_hash
+ result = {}
+ unless self == []
+ self.each do |item|
+ result[item[0]] = item[1]
+ end
+ end
+ result
+ end
+
+ def index_by
+ result = {}
+ self.each do |item|
+ result[yield item] = item
+ end
+ result
+ end
+
+ def subarray_count(rhs_array)
+ count = 0
+ if self == []
+ return count
+ end
+ if rhs_array == []
+ raise ArgumentError
+ end
+ self.each_index do |i|
+ if i > (self.length - rhs_array.length)
+ break
+ elsif self[i..i + rhs_array.length - 1] == rhs_array
+ count += 1
+ end
+ end
+ count
+ end
+
+ def occurences_count
+ result = Hash.new(0)
+ self.each do |item|
+ result[item] = self.count(item)
+ end
+ result
+ end
+end