Sha256: 303314558121826db37ee0f88239ad363a924731598af3885c785439f447a547

Contents?: true

Size: 827 Bytes

Versions: 1

Compression:

Stored size: 827 Bytes

Contents

module Mathmas
  def method_missing(name, *args)
    if /[a-zA-Z]/ =~ name && name.to_s.length == 1
      if args.length == 0
        if Mathmas.find_function(name).nil?
          return Mathmas::Variable.new(name)
        else
          return Mathmas.find_function(name)
        end
      else
        if args.all? {|arg| arg.is_a?(Numeric)}
          func = Mathmas.find_function(name)
          return func.exec(*args)
        else
          return Mathmas::Function.new(name, args)
        end
      end
    end
    super
  end

  def add_function(func)
    raise "The first argument should be an instance of Mathmas#Function" unless func.is_a?(Function)
    @@functions[func.name] = func
  end

  def find_function(name)
    @@functions[name]
  end

  @@functions = {}
  module_function :add_function, :find_function
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
mathmas-0.0.1 lib/mathmas/monkey.rb