Sha256: a7fe1a9a92daa3fd6ce1e7c5745e1984b51f2bf6cd101f54a3fc205177fe889f

Contents?: true

Size: 1.06 KB

Versions: 3

Compression:

Stored size: 1.06 KB

Contents

# frozen_string_literal: true

class Code
  class Object
    class Boolean < ::Code::Object
      def initialize(*args, **_kargs, &)
        raw = args.first || Nothing.new
        raw = raw.raw if raw.is_a?(Object)
        @raw = !!raw
      end

      def call(**args)
        operator = args.fetch(:operator, nil)
        arguments = args.fetch(:arguments, List.new)
        value = arguments.code_first

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

      def code_bitwise_and(value)
        Boolean.new(raw & value.raw)
      end

      def code_bitwise_or(value)
        Boolean.new(raw | value.raw)
      end

      def code_bitwise_xor(value)
        Boolean.new(raw ^ value.raw)
      end

      def truthy?
        raw
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
code-ruby-1.1.3 lib/code/object/boolean.rb
code-ruby-1.1.1 lib/code/object/boolean.rb
code-ruby-1.1.0 lib/code/object/boolean.rb