Class: Array
- Defined in:
- lib/reactive_extensions/array/scope.rb,
lib/reactive_extensions/object/sets.rb
Overview
Instance Method Summary collapse
-
#scope(key, *values) ⇒ Object
The
#scope
method is called on an array of hashes. -
#subset_of?(array) ⇒ Boolean
The
#subset_of?
method checks whether the calling array is a subset of the givenarray
. -
#superset_of?(array) ⇒ Boolean
The
#superset_of?
method checks whether the calling array is a superset of the givenarray
. -
#where_not(key, *values) ⇒ Object
The
#where_not
method is called on an array of hashes.
Instance Method Details
#scope(key, *values) ⇒ Object
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' }
]
32 33 34 |
# File 'lib/reactive_extensions/array/scope.rb', line 32 def scope(key, *values) self.select {|hash| values.include? hash[key] } end |
#subset_of?(array) ⇒ Boolean
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
57 58 59 60 61 |
# File 'lib/reactive_extensions/object/sets.rb', line 57 def subset_of?(array) raise ArgumentError.new("Argument of Array#subset_of? must be an array") unless array.instance_of? Array each {|i| return false unless array.include? i } true end |
#superset_of?(array) ⇒ Boolean
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
76 77 78 79 |
# File 'lib/reactive_extensions/object/sets.rb', line 76 def superset_of?(array) raise ArgumentError.new("Argument of Array#superset_of? must be an array") unless array.instance_of? Array array.subset_of? self end |
#where_not(key, *values) ⇒ Object
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' }
]
57 58 59 |
# File 'lib/reactive_extensions/array/scope.rb', line 57 def where_not(key, *values) self.reject {|hash| values.include? hash[key] } end |