Sha256: 30bb23a3b9e42fa9cf27adb1e0a64526f41e9463cacd47f125970d8377e824a6

Contents?: true

Size: 1.07 KB

Versions: 1

Compression:

Stored size: 1.07 KB

Contents

class QuadraticCommand < NarrativeMethodCommand
  class Error < Error; end

  def self.quadratic(a, b, c)
    d = b**2 - 4 * a * c
    case
    when d < 0
      []
    when d == 0
      [-b / (2 * a)]
    else
      [(-b + Math.sqrt(d)) / (2 * a), (-b - Math.sqrt(d)) / (2 * a)].sort
    end
  end

  # instance methods

  def validate
    log "Called #{self}.#{__method__}"
    raise Invalid, "liza quadratic 0 0 0" if @args.size != 3

    @a = @args[0].to_f
    @b = @args[1].to_f
    @c = @args[2].to_f

    log "@a = #{@a}"
    log "@b = #{@b}"
    log "@c = #{@c}"

    raise Invalid, "a can't be zero" if @a == 0
  end

  def perform
    log "Called #{self}.#{__method__}"

    @result = self.class.quadratic(@a, @b, @c)
    log "RESULT: #{@result}"
    log render "success.txt"
  end

end

__END__

# view help.txt.erb

NAME:

  liza quadratic

DESCRIPTION:

  Calculates the result of a quadratic equation.

USAGE:

  liza quadratic <a> <b> <c>

  where <a>, <b> and <c> are numbers.

EXAMPLES:

  liza quadratic 1 0 0
  liza quadratic 1 0 1
  liza quadratic 1 1 1
  liza quadratic 1 2 1

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
lizarb-1.0.4 app_new/dev/commands/quadratic_command.rb