Browse Source

Add comparison functions

master
Dylan Baker 5 years ago
parent
commit
8f652e87fb
1 changed files with 18 additions and 0 deletions
  1. 18
    0
      lib/chervil/core.rb

+ 18
- 0
lib/chervil/core.rb View File

@@ -1,10 +1,28 @@
1 1
 module Chervil
2 2
   module Core
3
+    def self.compare_pairs(ary, method)
4
+      pairs = Array.new
5
+      ary.each_with_index do |el, i|
6
+        unless ary[i + 1].nil?
7
+          pairs.push([el, ary[i + 1]])
8
+        end
9
+      end
10
+
11
+      !(pairs.map { |pair| pair[0].send(method, pair[1]) }.any?(false))
12
+    end
13
+
3 14
     CORE = {
4 15
       "+" => Proc.new { |args| args.inject(:+) },
5 16
       "-" => Proc.new { |args| args.inject(:-) },
6 17
       "*" => Proc.new { |args| args.inject(:*) },
7 18
       "/" => Proc.new { |args| args.inject(:/) },
19
+      "=" => Proc.new { |args| compare_pairs(args, :==) },
20
+      "<" => Proc.new { |args| compare_pairs(args, :<) },
21
+      ">" => Proc.new { |args| compare_pairs(args, :>) },
22
+      "<=" => Proc.new { |args| compare_pairs(args, :<=) },
23
+      ">=" => Proc.new { |args| compare_pairs(args, :>=) },
24
+      "and" => Proc.new { |args| !(args.include?(false)) },
25
+      "or" => Proc.new { |args| args.any? { |arg| !!arg == true } },
8 26
     }
9 27
   end
10 28
 end

Loading…
Cancel
Save