Sha256: b61b59f87a02b431538a9b22bf94c08bc3a61125504518fbf290e4f1c83837a2

Contents?: true

Size: 1.2 KB

Versions: 5

Compression:

Stored size: 1.2 KB

Contents

# frozen_string_literal: true

class Code
  class Object
    class Boolean < ::Code::Object
      def initialize(*args, **_kargs, &)
        @raw = (args.first.is_an?(Object) ? args.first.truthy? : !!args.first)
      end

      def call(**args)
        code_operator = args.fetch(:operator, nil).to_code
        code_arguments = args.fetch(:arguments, []).to_code
        code_value = code_arguments.code_first

        case code_operator.to_s
        when "&", "bitwise_and"
          sig(args) { Boolean }
          code_bitwise_and(code_value)
        when "^", "bitwise_xor"
          sig(args) { Boolean }
          code_bitwise_xor(code_value)
        when "|", "bitwise_or"
          sig(args) { Boolean }
          code_bitwise_or(code_value)
        else
          super
        end
      end

      def code_bitwise_and(other)
        code_other = other.to_code

        Boolean.new(raw & code_other.raw)
      end

      def code_bitwise_or(other)
        code_other = other.to_code

        Boolean.new(raw | code_other.raw)
      end

      def code_bitwise_xor(other)
        code_other = other.to_code

        Boolean.new(raw ^ code_other.raw)
      end

      def truthy?
        raw
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
code-ruby-1.2.5 lib/code/object/boolean.rb
code-ruby-1.2.4 lib/code/object/boolean.rb
code-ruby-1.2.3 lib/code/object/boolean.rb
code-ruby-1.2.2 lib/code/object/boolean.rb
code-ruby-1.2.1 lib/code/object/boolean.rb