Sha256: c133e11a986af2fe0a0b5b822127baaeaa91803d231f6392578adbe37c0997aa

Contents?: true

Size: 698 Bytes

Versions: 396

Compression:

Stored size: 698 Bytes

Contents

class Proverb
  attr_reader :chain, :options

  def initialize(*chain)
    if chain.last.is_a? Hash
      @options = chain.pop
    else
      @options = {}
    end
    @chain = chain
  end

  def to_s
    chain_of_events + conclusion
  end

  def chain_of_events
    causes_and_effects.map do |cause, effect|
      consequence(cause, effect)
    end.join("\n")
  end

  def causes_and_effects
    chain.each_cons(2)
  end

  def consequence(cause, effect)
    'For want of a %s the %s was lost.' % [cause, effect]
  end

  def qualifier
    options[:qualifier] ?  '%s ' % options[:qualifier] : ''
  end

  def conclusion
    "\nAnd all for the want of a %s%s." % [qualifier, chain.first]
  end
end

Version data entries

396 entries across 396 versions & 1 rubygems

Version Path
trackler-2.2.1.98 tracks/ruby/exercises/proverb/.meta/solutions/proverb.rb
trackler-2.2.1.97 tracks/ruby/exercises/proverb/.meta/solutions/proverb.rb
trackler-2.2.1.96 tracks/ruby/exercises/proverb/.meta/solutions/proverb.rb
trackler-2.2.1.95 tracks/ruby/exercises/proverb/.meta/solutions/proverb.rb
trackler-2.2.1.94 tracks/ruby/exercises/proverb/.meta/solutions/proverb.rb
trackler-2.2.1.93 tracks/ruby/exercises/proverb/.meta/solutions/proverb.rb
trackler-2.2.1.92 tracks/ruby/exercises/proverb/.meta/solutions/proverb.rb
trackler-2.2.1.91 tracks/ruby/exercises/proverb/.meta/solutions/proverb.rb
trackler-2.2.1.90 tracks/ruby/exercises/proverb/.meta/solutions/proverb.rb
trackler-2.2.1.89 tracks/ruby/exercises/proverb/.meta/solutions/proverb.rb
trackler-2.2.1.88 tracks/ruby/exercises/proverb/.meta/solutions/proverb.rb
trackler-2.2.1.87 tracks/ruby/exercises/proverb/.meta/solutions/proverb.rb
trackler-2.2.1.86 tracks/ruby/exercises/proverb/.meta/solutions/proverb.rb
trackler-2.2.1.85 tracks/ruby/exercises/proverb/.meta/solutions/proverb.rb
trackler-2.2.1.84 tracks/ruby/exercises/proverb/.meta/solutions/proverb.rb
trackler-2.2.1.83 tracks/ruby/exercises/proverb/.meta/solutions/proverb.rb
trackler-2.2.1.82 tracks/ruby/exercises/proverb/.meta/solutions/proverb.rb
trackler-2.2.1.81 tracks/ruby/exercises/proverb/.meta/solutions/proverb.rb
trackler-2.2.1.80 tracks/ruby/exercises/proverb/.meta/solutions/proverb.rb
trackler-2.2.1.79 tracks/ruby/exercises/proverb/.meta/solutions/proverb.rb