class Array

Public Instance Methods

from_serial(index = []) click to toggle source
# File lib/gamefic/serialize.rb, line 169
def from_serial(index = [])
  result = map { |e| e.from_serial(index) }
  result = "#<UNKNOWN>" if result.any? { |e| e == "#<UNKNOWN>" }
  result
end
join_and(sep = ', ', andSep = ' and ', serial = true) click to toggle source

Get a string representation of the array that separates elements with commas and adds a conjunction before the last element.

@example

animals = ['a dog', 'a cat', 'a mouse']
animals.join_and #=> 'a dog, a cat, and a mouse'

@param sep [String] The separator for all but the last element @param andSep [String] The separator for the last element @param serial [Boolean] Use serial separators (e.g., serial commas) @return [String]

# File lib/gamefic/core_ext/array.rb, line 51
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) click to toggle source

@see Array#join_and

@return [String]

# File lib/gamefic/core_ext/array.rb, line 63
def join_or(sep = ', ', orSep = ' or ', serial = true)
  join_and(sep, orSep, serial)
end
pop_sample() click to toggle source

Pop a random element from the array.

# File lib/gamefic/core_ext/array.rb, line 36
def pop_sample
  delete_at(rand(self.length))
end
that_are(*cls) click to toggle source

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.

@example

animals = ['dog', 'cat', nil]
animals.that_are(String) #=> ['dog', 'cat']
animals.that_are('dog')  #=> ['dog']
animals.that_are(:nil?)  #=> [nil]

@return [Array]

# 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) click to toggle source

Get a subset of the array that does not match the arguments. See Array#that_are for information about how arguments are evaluated.

@return [Array]

# 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(index = []) click to toggle source
# File lib/gamefic/serialize.rb, line 161
def to_serial(index = [])
  map do |e|
    s = e.to_serial(index)
    return "#<UNKNOWN>" if s == "#<UNKNOWN>"
    s
  end
end

Private Instance Methods

_keep(arr, cls, bool) click to toggle source
# File lib/gamefic/core_ext/array.rb, line 69
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