Class: Array
- Inherits:
-
Object
- Object
- Array
- Defined in:
- lib/gamefic/core_ext/array.rb
Instance Method Summary collapse
-
#join_and(sep = ', ', andSep = ' and ', serial = true) ⇒ String
Get a string representation of the array that separates elements with commas and adds a conjunction before the last element.
- #join_or(sep = ', ', orSep = ' or ', serial = true) ⇒ String
-
#pop_random ⇒ Object
deprecated
Deprecated.
Use Array#pop_sample instead.
-
#pop_sample ⇒ Object
Pop a random element from the array.
-
#random ⇒ Object
deprecated
Deprecated.
Use Array#sample instead.
-
#that_are(cls) ⇒ Array
Get a subset of the array that matches the argument.
-
#that_are_not(cls) ⇒ Array
Get a subset of the array that does not match the argument.
Instance Method Details
#join_and(sep = ', ', andSep = ' and ', serial = true) ⇒ String
Get a string representation of the array that separates elements with commas and adds a conjunction before the last element.
85 86 87 88 89 90 91 92 |
# File 'lib/gamefic/core_ext/array.rb', line 85 def join_and(sep = ', ', andSep = ' and ', serial = true) if self.length < 3 self.join(andSep) else start = self[0..-2] start.join(sep) + "#{serial ? sep.strip : ''}#{andSep}#{self.last}" end end |
#join_or(sep = ', ', orSep = ' or ', serial = true) ⇒ String
97 98 99 |
# File 'lib/gamefic/core_ext/array.rb', line 97 def join_or(sep = ', ', orSep = ' or ', serial = true) join_and(sep, orSep, serial) end |
#pop_random ⇒ Object
Use Array#pop_sample instead.
Pop a random element from the array.
51 52 53 |
# File 'lib/gamefic/core_ext/array.rb', line 51 def pop_random pop_sample end |
#pop_sample ⇒ Object
Pop a random element from the array.
57 58 59 |
# File 'lib/gamefic/core_ext/array.rb', line 57 def pop_sample delete_at(rand(self.length)) end |
#random ⇒ Object
Use Array#sample instead.
Get a random element from the array.
44 45 46 |
# File 'lib/gamefic/core_ext/array.rb', line 44 def random return self[rand(self.length)] end |
#that_are(cls) ⇒ Array
Get a subset of the array that matches the argument. If the argument is a Class or Module, the elements must be of the type. If the argument is a Symbol, it should be a method for which the elements must return true. If the argument is an Object, the elements must equal the object.
14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/gamefic/core_ext/array.rb', line 14 def that_are(cls) if (cls.kind_of?(Class) or cls.kind_of?(Module)) return self.clone.delete_if { |i| i.kind_of?(cls) == false } elsif cls.kind_of?(Symbol) return self.clone.delete_if { |i| i.send(cls) == false } else if self.include?(cls) return [cls] end return Array.new end end |
#that_are_not(cls) ⇒ Array
Get a subset of the array that does not match the argument. See Array#that_are for information about how arguments are evaluated.
31 32 33 34 35 36 37 38 39 |
# File 'lib/gamefic/core_ext/array.rb', line 31 def that_are_not(cls) if (cls.kind_of?(Class) or cls.kind_of?(Module)) return self.clone.delete_if { |i| i.kind_of?(cls) == true } elsif cls.kind_of?(Symbol) return self.clone.delete_if { |i| i.send(cls) == true } else return self.clone - [cls] end end |