class Array
Ruby’s core Array
class. See documentation for version 2.1.5, 2.0.0, or 1.9.3.
Public Instance Methods
The #scope
method is called on an array of hashes. It returns a sub-array including only hashes for which the value at a given key
is among the given values
. The #scope
method is non-destructive; the original array will remain intact after it is called. The #scope
method is known to work for string or symbol keys. It should work for other data type keys as well.
Example:
array = [ { name: 'Jean-Paul Sartre', nationality: 'French' }, { name: 'Bertrand Russell', nationality: 'English' }, { name: 'Ludwig Wittgenstein', nationality: 'Austrian' }, { name: 'Albert Camus', nationality: 'French' } ] array.scope(:nationality, 'French') # => [ { name: 'Jean-Paul Sartre', nationality: 'French' }, { name: 'Albert Camus', nationality: 'French' } ]
# File lib/reactive_extensions/array/scope.rb 32 def scope(key, *values) 33 self.select {|hash| values.include? hash[key] } 34 end
The #subset_of?
method checks whether the calling array is a subset of the given array
. It returns true if all objects in the calling array are also present in the hash passed as an argument, regardless of order. If the calling array is empty, the method returns true.
Examples:
array = [1, 2, 3, 4] [1, 3].subset_of? array # => true [1, 6].subset_of? array # => false [].subset_of? array # => true array.subset_of? 'foobar' # => ArgumentError
# File lib/reactive_extensions/object/sets.rb 57 def subset_of?(array) 58 raise ArgumentError.new("Argument of Array#subset_of? must be an array") unless array.instance_of? Array 59 each {|i| return false unless array.include? i } 60 true 61 end
The #superset_of?
method checks whether the calling array is a superset of the given array
. It returns true if all objects in the calling array are also present in the array passed as an argument. If the argument array is empty, the method returns true.
Examples:
array = [1, 2, 3, 4] array.superset_of? [1, 2] # => true array.superset_of? [1, 2, 3, 4] # => true array.superset_of? [3, 6] # => false array.superset_of? [] # => true array.superset_of? {} # => ArgumentError
# File lib/reactive_extensions/object/sets.rb 76 def superset_of?(array) 77 raise ArgumentError.new("Argument of Array#superset_of? must be an array") unless array.instance_of? Array 78 array.subset_of? self 79 end
The #where_not
method is called on an array of hashes. It returns a sub-array including only hashes for which the value at a given key
is not among the given values
. It is the inverse of the #scope
method. The #where_not
method is non-destructive; the original array will remain intact after it is called. The #where_not
method is known to work for string or symbol keys. It should work for other data types as well.
Example:
array = [ { name: 'Jean-Paul Sartre', nationality: 'French' }, { name: 'Bertrand Russell', nationality: 'English' }, { name: 'Ludwig Wittgenstein', nationality: 'Austrian' }, { name: 'Albert Camus', nationality: 'French' } ] array.where_not(:nationality, 'French') # => [ { name: 'Bertrand Russell', nationality: 'English' }, { name: 'Ludwig Wittgenstein', nationality: 'English' } ]
# File lib/reactive_extensions/array/scope.rb 57 def where_not(key, *values) 58 self.reject {|hash| values.include? hash[key] } 59 end