summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDan Allen <dan.j.allen@gmail.com>2015-07-01 23:44:33 -0600
committerDan Allen <dan.j.allen@gmail.com>2015-07-02 00:25:25 -0600
commitdd4886ecc6dfa184c3f699acd35707e8fcbc1983 (patch)
tree286ca96b6be6c97594480a2d7e198f62ea2fd843
parent1d5041ba13db7dd9aba0af9f1d2aa898e2667183 (diff)
resolves #1385 add implementation of != method to Opal
- add implementation of != - apply code styles
-rw-r--r--lib/asciidoctor/opal_ext/comparable.rb48
1 files changed, 33 insertions, 15 deletions
diff --git a/lib/asciidoctor/opal_ext/comparable.rb b/lib/asciidoctor/opal_ext/comparable.rb
index 7c3a85ee..98f39753 100644
--- a/lib/asciidoctor/opal_ext/comparable.rb
+++ b/lib/asciidoctor/opal_ext/comparable.rb
@@ -1,38 +1,56 @@
-# workaround for an infinite loop in Opal 0.6.2 when comparing numbers
+class BasicObject
+ # Provides implementation for missing != method BasicObject. Allows the
+ # method :!= to be sent to an object.
+ def != other
+ `self !== other`
+ end
+end
+
+# workaround for an infinite loop in Opal 0.6.x when comparing numbers
module Comparable
def == other
return true if equal? other
- return false unless cmp = (self <=> other)
- return `cmp == 0`
+ # if <=> returns nil, assume these objects can't be compared (and thus not equal)
+ return false unless res = (self <=> other)
+ return `res == 0`
rescue StandardError
false
end
+ def != other
+ return false if equal? other
+ # if <=> returns nil, assume these objects can't be compared (and thus not equal)
+ return true unless res = (self <=> other)
+ return `res != 0`
+ rescue StandardError
+ true
+ end
+
def > other
- unless cmp = (self <=> other)
- raise ArgumentError, "comparison of #{self.class} with #{other.class} failed"
+ unless res = (self <=> other)
+ raise ArgumentError, %(comparison of #{self.class} with #{other.class} failed)
end
- `cmp > 0`
+ `res > 0`
end
def >= other
- unless cmp = (self <=> other)
- raise ArgumentError, "comparison of #{self.class} with #{other.class} failed"
+ unless res = (self <=> other)
+ raise ArgumentError, %(comparison of #{self.class} with #{other.class} failed)
end
- `cmp >= 0`
+ `res >= 0`
end
def < other
- unless cmp = (self <=> other)
- raise ArgumentError, "comparison of #{self.class} with #{other.class} failed"
+ unless res = (self <=> other)
+ raise ArgumentError, %(comparison of #{self.class} with #{other.class} failed)
end
- `cmp < 0`
+ `res < 0`
end
def <= other
- unless cmp = (self <=> other)
- raise ArgumentError, "comparison of #{self.class} with #{other.class} failed"
+ unless res = (self <=> other)
+ raise ArgumentError, %(comparison of #{self.class} with #{other.class} failed)
end
- `cmp <= 0`
+ `res <= 0`
end
end