module Sequel::SQL::NumericMethods
This module includes the standard mathematical methods (+, -, *, and /) that are defined on objects that can be used in a numeric context in SQL
(Symbol
, LiteralString
, and SQL::GenericExpression
).
:a + :b # "a" + "b" :a - :b # "a" - "b" :a * :b # "a" * "b" :a / :b # "a" / "b"
One exception to this is if + is called with a String
or StringExpression
, in which case the || operator is used instead of the + operator:
Sequel[:a] + 'b' # "a" || 'b'
Public Instance Methods
+(ce)
click to toggle source
Use || as the operator when called with StringExpression
and String
instances, and the + operator for LiteralStrings and all other types.
# File lib/sequel/sql.rb 776 def +(ce) 777 case ce 778 when LiteralString 779 NumericExpression.new(:+, self, ce) 780 when StringExpression, String 781 StringExpression.new(:'||', self, ce) 782 else 783 NumericExpression.new(:+, self, ce) 784 end 785 end
coerce(other)
click to toggle source
If the argument given is Numeric
, treat it as a NumericExpression
, allowing code such as:
1 + Sequel[:x] # SQL: (1 + x) Sequel.expr{1 - x(y)} # SQL: (1 - x(y))
Calls superclass method
# File lib/sequel/sql.rb 766 def coerce(other) 767 if other.is_a?(Numeric) 768 [SQL::NumericExpression.new(:NOOP, other), self] 769 else 770 super 771 end 772 end