Class: BCDice::CommonCommand::Calc::Node::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/bcdice/common_command/calc/node.rb

Instance Method Summary collapse

Constructor Details

#initialize(secret:, expr:) ⇒ Command

Returns a new instance of Command.



10
11
12
13
# File 'lib/bcdice/common_command/calc/node.rb', line 10

def initialize(secret:, expr:)
  @secret = secret
  @expr = expr
end

Instance Method Details

#eval(round_type) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/bcdice/common_command/calc/node.rb', line 15

def eval(round_type)
  value =
    begin
      @expr.eval(round_type)
    rescue ZeroDivisionError
      "ゼロ除算が発生したため計算できませんでした"
    end

  output =
    if @expr.is_a?(Arithmetic::Node::Parenthesis)
      @expr.output
    else
      "(#{@expr.output})"
    end

  Result.new.tap do |r|
    r.secret = @secret
    r.text = "c#{output}#{value}"
  end
end