Class: Array
- Defined in:
- lib/story_teller/mixins.rb,
lib/story_teller/mixins.rb
Overview
The Array class
Direct Known Subclasses
Constant Summary collapse
- Empty =
EmptyImmutableEnumerable.new(Array)
Instance Method Summary collapse
- #+(other) ⇒ Object
- #average ⇒ Object
- #having(method) ⇒ Object
- #identities ⇒ Object
- #longest_length ⇒ Object
- #pad(min = 1, max = longest_length + min) ⇒ Object
- #plus_operator ⇒ Object
- #ruby_to_s ⇒ Object
- #sum(init = nil) ⇒ Object
- #to_s(separator = CommaSpaceString) ⇒ Object
- #to_sentence(conjunction = 'and') ⇒ Object
Instance Method Details
#+(other) ⇒ Object
453 454 455 456 457 458 |
# File 'lib/story_teller/mixins.rb', line 453 def +(other) case other when String then self.to_s + other else plus_operator(other) end end |
#average ⇒ Object
471 472 473 474 |
# File 'lib/story_teller/mixins.rb', line 471 def average return self.first if self.length == 1 (self.sum / self.length.to_f) end |
#having(method) ⇒ Object
476 477 478 |
# File 'lib/story_teller/mixins.rb', line 476 def having(method) select { |a| a.respond_to? method.to_sym } end |
#identities ⇒ Object
480 481 482 |
# File 'lib/story_teller/mixins.rb', line 480 def identities map { |o| "#{o} [#{o.identity}]" } end |
#longest_length ⇒ Object
484 485 486 487 488 |
# File 'lib/story_teller/mixins.rb', line 484 def longest_length inject(0) do |max, x| [x.to_s.length, max].max end end |
#pad(min = 1, max = longest_length + min) ⇒ Object
490 491 492 493 494 |
# File 'lib/story_teller/mixins.rb', line 490 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_operator ⇒ Object
452 |
# File 'lib/story_teller/mixins.rb', line 452 alias plus_operator + |
#ruby_to_s ⇒ Object
507 |
# File 'lib/story_teller/mixins.rb', line 507 alias ruby_to_s to_s |
#sum(init = nil) ⇒ Object
460 461 462 463 464 465 466 467 468 469 |
# File 'lib/story_teller/mixins.rb', line 460 def sum(init = nil) if block_given? initial = init.nil? ? 0 : init inject(initial) { |total, element| total + yield(element) } elsif init.nil? inject(:+) else inject(init, :+) end end |
#to_s(separator = CommaSpaceString) ⇒ Object
508 509 510 511 512 |
# File 'lib/story_teller/mixins.rb', line 508 def to_s(separator = CommaSpaceString) # This will make it possible to print Lists nicely, but not screw up # the behavior of the List. self.join(separator) end |
#to_sentence(conjunction = 'and') ⇒ Object
496 497 498 499 500 501 502 503 504 505 |
# File 'lib/story_teller/mixins.rb', line 496 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 |