Class: Variable

Inherits:
Object
  • Object
show all
Defined in:
lib/chebyruby/variable.rb

Overview

This is the class for a variable in an expression. It is part of the CAS side-project in ChebyRuby.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(x, neg = false) ⇒ Variable

This is the constructor for the variable class

Parameters:

  • x (String)

    the variable name to initialize

  • neg (Boolean) (defaults to: false)

    the negation status of the variable



15
16
17
18
# File 'lib/chebyruby/variable.rb', line 15

def initialize(x, neg = false)
  @x = x
  @neg = false
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object

This variadic method missing works with Expression’s method missing to construct and expression from variables.

Parameters:

  • method (Object)

    the method that is missing

  • args (Object[])

    the args that are passed to the missing method

Returns:

  • a new expression



26
27
28
# File 'lib/chebyruby/variable.rb', line 26

def method_missing(method, *args)
  Expression.new(self, method, Variable.new(args[0]))
end

Instance Attribute Details

#negBoolean

the negation status of the variable

Returns:

  • (Boolean)

    the current value of neg



8
9
10
# File 'lib/chebyruby/variable.rb', line 8

def neg
  @neg
end

#xString

the variable name

Returns:

  • (String)

    the current value of x



8
9
10
# File 'lib/chebyruby/variable.rb', line 8

def x
  @x
end

Instance Method Details

#-@Object

This is an overriding of the unary negation operation that allows for negation of a variable as simply as -x.

Returns:

  • a negated form of the current variable



45
46
47
# File 'lib/chebyruby/variable.rb', line 45

def -@
  Variable.new(@x, !@neg)
end

#parseableObject

Returns a parseable version of the variable/expression for computer system modification.

Returns:

  • parseable form



74
75
76
77
78
79
80
81
82
83
84
# File 'lib/chebyruby/variable.rb', line 74

def parseable
  to_enum.map do |i|
    if Array === i
      i.parseable
    elsif Symbol === i
      i
    else
      i.x
    end
  end
end

#rightObject

A nil returning function for the purposes of expression building

Returns:

  • nil



59
60
61
# File 'lib/chebyruby/variable.rb', line 59

def right
  nil
end

#to_aObject Also known as: to_ary

This method will turn a variadic variable into an array

Returns:

  • an array of the variable



33
34
35
36
37
38
39
# File 'lib/chebyruby/variable.rb', line 33

def to_a
  if x.class == Array
    x
  else
    [x]
  end
end

#to_enumObject

This turns the current array variable into an enumerator

Returns:

  • an enumerated form of the array variable



52
53
54
# File 'lib/chebyruby/variable.rb', line 52

def to_enum
  Array.new(to_ary).to_enum
end

#to_sObject

A basic to_s function

Returns:

  • a string



66
67
68
# File 'lib/chebyruby/variable.rb', line 66

def to_s
  "#{x}"
end