Class: Variable
- Inherits:
-
Object
- Object
- Variable
- 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
-
#neg ⇒ Boolean
the negation status of the variable.
-
#x ⇒ String
the variable name.
Instance Method Summary collapse
-
#-@ ⇒ Object
This is an overriding of the unary negation operation that allows for negation of a variable as simply as -x.
-
#initialize(x, neg = false) ⇒ Variable
constructor
This is the constructor for the variable class.
-
#method_missing(method, *args) ⇒ Object
This variadic method missing works with Expression’s method missing to construct and expression from variables.
-
#parseable ⇒ Object
Returns a parseable version of the variable/expression for computer system modification.
-
#right ⇒ Object
A nil returning function for the purposes of expression building.
-
#to_a ⇒ Object
(also: #to_ary)
This method will turn a variadic variable into an array.
-
#to_enum ⇒ Object
This turns the current array variable into an enumerator.
-
#to_s ⇒ Object
A basic to_s function.
Constructor Details
#initialize(x, neg = false) ⇒ Variable
This is the constructor for the variable class
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.
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
#neg ⇒ Boolean
the negation status of the variable
8 9 10 |
# File 'lib/chebyruby/variable.rb', line 8 def neg @neg end |
#x ⇒ String
the variable name
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.
45 46 47 |
# File 'lib/chebyruby/variable.rb', line 45 def -@ Variable.new(@x, !@neg) end |
#parseable ⇒ Object
Returns a parseable version of the variable/expression for computer system modification.
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 |
#right ⇒ Object
A nil returning function for the purposes of expression building
59 60 61 |
# File 'lib/chebyruby/variable.rb', line 59 def right nil end |
#to_a ⇒ Object Also known as: to_ary
This method will turn a variadic variable into an array
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_enum ⇒ Object
This turns the current array variable into an enumerator
52 53 54 |
# File 'lib/chebyruby/variable.rb', line 52 def to_enum Array.new(to_ary).to_enum end |
#to_s ⇒ Object
A basic to_s function
66 67 68 |
# File 'lib/chebyruby/variable.rb', line 66 def to_s "#{x}" end |