Class: Array
- Defined in:
- lib/gamefic/serialize.rb,
lib/gamefic/core_ext/array.rb
Instance Method Summary collapse
-
#_keep(arr, cls, bool) ⇒ Object
private.
-
#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_sample ⇒ Object
Pop a random element from the array.
-
#that_are(*cls) ⇒ Array
Get a subset of the array that matches the arguments.
-
#that_are_not(*cls) ⇒ Array
Get a subset of the array that does not match the arguments.
- #to_serial ⇒ Object
Instance Method Details
#_keep(arr, cls, bool) ⇒ Object
private
66 67 68 69 70 71 72 73 74 |
# File 'lib/gamefic/core_ext/array.rb', line 66 def _keep(arr, cls, bool) if (cls.kind_of?(Class) or cls.kind_of?(Module)) arr.keep_if { |i| i.is_a?(cls) == bool } elsif cls.kind_of?(Symbol) arr.keep_if { |i| i.send(cls) == bool } else arr.keep_if {|i| (i == cls) == bool} end end |
#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.
48 49 50 51 52 53 54 55 |
# File 'lib/gamefic/core_ext/array.rb', line 48 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
60 61 62 |
# File 'lib/gamefic/core_ext/array.rb', line 60 def join_or(sep = ', ', orSep = ' or ', serial = true) join_and(sep, orSep, serial) end |
#pop_sample ⇒ Object
Pop a random element from the array.
36 37 38 |
# File 'lib/gamefic/core_ext/array.rb', line 36 def pop_sample delete_at(rand(self.length)) end |
#that_are(*cls) ⇒ Array
Get a subset of the array that matches the arguments. 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 |
# File 'lib/gamefic/core_ext/array.rb', line 14 def that_are(*cls) result = self.clone cls.each do |c| _keep result, c, true end result end |
#that_are_not(*cls) ⇒ Array
Get a subset of the array that does not match the arguments. See Array#that_are for information about how arguments are evaluated.
26 27 28 29 30 31 32 |
# File 'lib/gamefic/core_ext/array.rb', line 26 def that_are_not(*cls) result = self.clone cls.each do |c| _keep result, c, false end result end |
#to_serial ⇒ Object
48 49 50 51 52 53 54 |
# File 'lib/gamefic/serialize.rb', line 48 def to_serial map do |e| s = e.to_serial return "#<UNKNOWN>" if s == "#<UNKNOWN>" s end end |