Class: Array
- Inherits:
-
Object
- Object
- Array
- Defined in:
- lib/gamefic/core_ext/array.rb
Direct Known Subclasses
Instance Method Summary collapse
-
#join_and(sep = ', ', andSep = ' and ', serial = true) ⇒ String
Get a string representation of the array that separated elements with commas and adds a conjunction before the last element.
- #join_or(sep = ', ', orSep = ' or ', serial = true) ⇒ String
- #pop_random ⇒ Object
- #random ⇒ Object
- #shuffle ⇒ Array
- #shuffle! ⇒ Array
- #that_are(cls) ⇒ Array
- #that_are_not(cls) ⇒ Array
Instance Method Details
#join_and(sep = ', ', andSep = ' and ', serial = true) ⇒ String
Get a string representation of the array that separated elements with commas and adds a conjunction before the last element.
46 47 48 49 50 51 52 53 |
# File 'lib/gamefic/core_ext/array.rb', line 46 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
55 56 57 |
# File 'lib/gamefic/core_ext/array.rb', line 55 def join_or(sep = ', ', orSep = ' or ', serial = true) join_and(sep, orSep, serial) end |
#pop_random ⇒ Object
28 29 30 |
# File 'lib/gamefic/core_ext/array.rb', line 28 def pop_random delete_at(rand(self.length)) end |
#random ⇒ Object
25 26 27 |
# File 'lib/gamefic/core_ext/array.rb', line 25 def random return self[rand(self.length)] end |
#shuffle ⇒ Array
32 33 34 35 36 |
# File 'lib/gamefic/core_ext/array.rb', line 32 def shuffle self.sort { |a, b| rand(3) <=> rand(3) } end |
#shuffle! ⇒ Array
38 39 40 41 42 |
# File 'lib/gamefic/core_ext/array.rb', line 38 def shuffle! self.sort! { |a, b| rand(3) <=> rand(3) } end |
#that_are(cls) ⇒ Array
3 4 5 6 7 8 9 10 11 12 13 14 |
# File 'lib/gamefic/core_ext/array.rb', line 3 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
16 17 18 19 20 21 22 23 24 |
# File 'lib/gamefic/core_ext/array.rb', line 16 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 |