lib/active_object/array.rb in active_object-5.1.1 vs lib/active_object/array.rb in active_object-5.1.2
- old
+ new
@@ -35,10 +35,18 @@
result = []
args.each { |val| result << delete(val) }
result
end
+ def demote(value)
+ sort_by { |val| val == value ? 0 : -1 }
+ end
+
+ def demote!(value)
+ replace(demote(value))
+ end
+
def denillify(value = 0)
map { |val| val.nil? ? value : val }
end
def denillify!(value = 0)
@@ -115,10 +123,16 @@
block_given? ? sliced_collection { |val| yield(val) } : sliced_collection.to_a
end
# rubocop:enable Metrics/MethodLength, Metrics/AbcSize
+ def indexes(value)
+ results = []
+ each_with_index { |val, i| results << i if value == val }
+ results
+ end
+
def merge(*values)
values.each { |val| concat(val) }
self
end
@@ -128,10 +142,19 @@
def nillify!
replace(nillify)
end
+ def position(n)
+ idx = index(n)
+ (idx + 1) unless idx.nil?
+ end
+
+ def positions(value)
+ indexes(value).map { |val| val + 1 }
+ end
+
def probability
hash = ::Hash.new(0.0)
differ = 0.0
each do |val|
@@ -141,11 +164,24 @@
hash.each_key { |val| hash[val] /= differ }
hash
end
+ def promote(value)
+ sort_by { |val| val == value ? -1 : 0 }
+ end
+
+ def promote!(value)
+ replace(promote(value))
+ end
+
def reject_values(*args)
reject { |val| args.include?(val) }
+ end
+
+ def rposition(n)
+ idx = rindex(n)
+ (idx + 1) unless idx.nil?
end
def sample!
delete_at(::Random.rand(length - 1))
end