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.180 tracks/ruby/exercises/proverb/.meta/solutions/proverb.rb
trackler-2.2.1.179 tracks/ruby/exercises/proverb/.meta/solutions/proverb.rb
trackler-2.2.1.178 tracks/ruby/exercises/proverb/.meta/solutions/proverb.rb
trackler-2.2.1.177 tracks/ruby/exercises/proverb/.meta/solutions/proverb.rb
trackler-2.2.1.176 tracks/ruby/exercises/proverb/.meta/solutions/proverb.rb
trackler-2.2.1.175 tracks/ruby/exercises/proverb/.meta/solutions/proverb.rb
trackler-2.2.1.174 tracks/ruby/exercises/proverb/.meta/solutions/proverb.rb
trackler-2.2.1.173 tracks/ruby/exercises/proverb/.meta/solutions/proverb.rb
trackler-2.2.1.172 tracks/ruby/exercises/proverb/.meta/solutions/proverb.rb
trackler-2.2.1.171 tracks/ruby/exercises/proverb/.meta/solutions/proverb.rb
trackler-2.2.1.170 tracks/ruby/exercises/proverb/.meta/solutions/proverb.rb
trackler-2.2.1.169 tracks/ruby/exercises/proverb/.meta/solutions/proverb.rb
trackler-2.2.1.167 tracks/ruby/exercises/proverb/.meta/solutions/proverb.rb
trackler-2.2.1.166 tracks/ruby/exercises/proverb/.meta/solutions/proverb.rb
trackler-2.2.1.165 tracks/ruby/exercises/proverb/.meta/solutions/proverb.rb
trackler-2.2.1.164 tracks/ruby/exercises/proverb/.meta/solutions/proverb.rb
trackler-2.2.1.163 tracks/ruby/exercises/proverb/.meta/solutions/proverb.rb
trackler-2.2.1.162 tracks/ruby/exercises/proverb/.meta/solutions/proverb.rb
trackler-2.2.1.161 tracks/ruby/exercises/proverb/.meta/solutions/proverb.rb
trackler-2.2.1.160 tracks/ruby/exercises/proverb/.meta/solutions/proverb.rb