Class: Array

Inherits:
Object show all
Defined in:
lib/runtime/mixins.rb,
lib/runtime/mixins.rb

Overview

The Array class

Direct Known Subclasses

Inform::Verb::Grammar

Constant Summary collapse

Empty =
EmptyImmutableEnumerable.new(Array)

Instance Method Summary collapse

Instance Method Details

#+(other) ⇒ Object



452
453
454
455
456
457
# File 'lib/runtime/mixins.rb', line 452

def +(other)
  case other
  when String then self.to_s + other
  else plus_operator(other)
  end
end

#averageObject



464
465
466
467
# File 'lib/runtime/mixins.rb', line 464

def average
  return self.first if self.length == 1
  (self.sum / self.length.to_f)
end

#having(method) ⇒ Object



469
470
471
# File 'lib/runtime/mixins.rb', line 469

def having(method)
  select { |a| a.respond_to? method.to_sym }
end

#identitiesObject



473
474
475
# File 'lib/runtime/mixins.rb', line 473

def identities
  map { |o| "#{o} [#{o.identity}]" }
end

#longest_lengthObject



477
478
479
480
481
# File 'lib/runtime/mixins.rb', line 477

def longest_length
  inject(0) do |max, x|
    [x.to_s.length, max].max
  end
end

#pad(min = 1, max = longest_length + min) ⇒ Object



483
484
485
486
487
# File 'lib/runtime/mixins.rb', line 483

def pad(min = 1, max = longest_length + min)
  each do |x|
    yield(format(JoinedTemplate, str: x.to_s, other: ' ' * [max - x.to_s.length, min].max), x)
  end
end

#plus_operatorObject



451
# File 'lib/runtime/mixins.rb', line 451

alias plus_operator +

#ruby_to_sObject



500
# File 'lib/runtime/mixins.rb', line 500

alias ruby_to_s to_s

#sum(&block) ⇒ Object



459
460
461
462
# File 'lib/runtime/mixins.rb', line 459

def sum(&block)
  return inject(0, &block) if block_given?
  inject(:+)
end

#to_s(separator = CommaSpaceString) ⇒ Object



501
502
503
504
505
# File 'lib/runtime/mixins.rb', line 501

def to_s(separator = CommaSpaceString)
  # This will make it possible to print Lists nicely, but not screw up
  # the behavior of the List.
  map(&:to_s).join(separator)
end

#to_sentence(conjunction = 'and') ⇒ Object



489
490
491
492
493
494
495
496
497
498
# File 'lib/runtime/mixins.rb', line 489

def to_sentence(conjunction = 'and')
  case length
  when 0 then ''
  when 1 then self.first.dup.to_s
  when 2
    format(SentenceTemplate, str: self[0], other: conjunction, last: self[1])
  else
    format(SentenceTemplate, str: self[0...].join(CommaSpaceString), other: conjunction, last: self[-1])
  end
end