Class: Robut::Plugin::Calc

Inherits:
Object
  • Object
show all
Includes:
Robut::Plugin
Defined in:
lib/robut/plugin/calc.rb

Overview

A simple calculator. This delegates all calculations to the ‘calc’ gem.

Instance Attribute Summary

Attributes included from Robut::Plugin

#connection, #private_sender, #reply_to

Instance Method Summary collapse

Methods included from Robut::Plugin

#at_nick, #fake_message, included, #initialize, #nick, #reply, #sent_to_me?, #store, #without_nick, #words

Instance Method Details

#handle(time, sender_nick, message) ⇒ Object

Perform the calculation specified in message, and send the result back.



15
16
17
18
19
20
21
22
23
24
# File 'lib/robut/plugin/calc.rb', line 15

def handle(time, sender_nick, message)
  if sent_to_me?(message) && words(message).first == 'calc'
    calculation = words(message, 'calc').join(' ')
    begin
      reply("#{calculation} = #{::Calc.evaluate(calculation)}")
    rescue
      reply("Can't calculate that.")
    end
  end
end

#usageObject

Returns a description of how to use this plugin



9
10
11
# File 'lib/robut/plugin/calc.rb', line 9

def usage
  "#{at_nick} calc <calculation> - replies with the result of <calculation>"
end