Мартин обнови решението на 24.10.2011 02:06 (преди около 13 години)
+=begin
+The best solution is:
+
+class Array
+ def to_hash
+ Hash[*self.flatten(1)]
+ end
+end
+
+I haven't posted it because I'm afraid a lot of people are going
+to use it in their code.
+=end
+class Array
+ def to_hash
+ new_hash = {}
+ self.each { | key, value | new_hash[key] = value }
+ new_hash
+ end
+
+ def index_by
+ new_hash = {}
+ self.each do | value |
+ key = yield value # yield is a lot faster than block.call()
+ new_hash[key] = value
+ end
+ new_hash
+ end
+
+ def subarray_count(subarray)
+ string_array = self.join('') # Converting into string
+ string_subarray = subarray.join('') # Converting into string
+ pattern = "(?=(#{string_subarray}))"
+ string_array.scan(Regexp.new(pattern)).length # Using Regexp
+ end
+
+ def occurences_count
+ new_hash = Hash.new(0)
+ self.each { |el| new_hash[el] += 1 }
+ new_hash
+ end
+end