Class: CeilingCat::Plugin::Calc

Inherits:
Base
  • Object
show all
Defined in:
lib/ceiling_cat/plugins/calc.rb

Instance Attribute Summary

Attributes inherited from Base

#event

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#body, #body_without_command, #body_without_nick, #body_without_nick_or_command, #commands, #handle, #initialize, #pluralize, #reply, #room, store, #store, #user, #words

Constructor Details

This class inherits a constructor from CeilingCat::Plugin::Base

Class Method Details

.commandsObject


4
5
6
# File 'lib/ceiling_cat/plugins/calc.rb', line 4

def self.commands
  [{:command => "calculate", :description => "Performs basic math functions - '!calculate 7*5'", :method => "calculate", :public => true}]
end

.descriptionObject


8
9
10
# File 'lib/ceiling_cat/plugins/calc.rb', line 8

def self.description
  "A basic calculator, for all your mathin' needs!"
end

.nameObject


12
13
14
# File 'lib/ceiling_cat/plugins/calc.rb', line 12

def self.name
  "Calculator"
end

.public?Boolean

Returns:

  • (Boolean)

16
17
18
# File 'lib/ceiling_cat/plugins/calc.rb', line 16

def self.public?
  true
end

Instance Method Details

#calculateObject


20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/ceiling_cat/plugins/calc.rb', line 20

def calculate
  begin
    math = body.gsub(/[^\d+-\/*\(\)\.]/,"") # Removing anything but numbers, operators, and parens
    if math.length > 0 && math =~ /^\d.+\d$/
      reply "#{math} = #{eval math}"
    else
      reply "I don't think that's an equation. Want to try something else?"
    end
  rescue => e
    raise e
  end
end