Skip to content

Commit 70cb270

Browse files
author
Ary Borenszweig
committed
Fixed comparison (convert operands to numbers instead of to string)
1 parent e262f65 commit 70cb270

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

lib/rkelly/visitors/evaluation_visitor.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -473,9 +473,9 @@ def additive_operator(operator, left, right)
473473

474474
def compare(left, right, is_equal_operator = false)
475475
if left.is_a?(String) && right.is_a?(Numeric)
476-
right = right.to_s
476+
left = Float(left) rescue left
477477
elsif left.is_a?(Numeric) && right.is_a?(String)
478-
left = left.to_s
478+
right = Float(right) rescue right
479479
elsif left.nil? && right.is_a?(Numeric)
480480
left = 0
481481
if is_equal_operator && right == 0

test/test_eval.rb

+8
Original file line numberDiff line numberDiff line change
@@ -128,4 +128,12 @@ def test_integer_null_comparison_3
128128
def test_integer_null_comparison_4
129129
assert !@runtime.eval("0 == null")
130130
end
131+
132+
def test_string_equality_with_leading_zero
133+
assert @runtime.eval('"01" == 1')
134+
end
135+
136+
def test_string_equality_with_leading_zero
137+
assert !@runtime.eval('"hola" == 0')
138+
end
131139
end

0 commit comments

Comments
 (0)