Sha256: a3e80cb993b76dbe17f133a854b57e7d1c6273f2390b13181e19232b46c5a06f

Contents?: true

Size: 1.06 KB

Versions: 2

Compression:

Stored size: 1.06 KB

Contents

# frozen_string_literal: true

class Code
  class Object
    class Boolean < ::Code::Object
      def initialize(*args, **_kargs, &_block)
        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, [])
        value = arguments.first&.value

        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

2 entries across 2 versions & 1 rubygems

Version Path
code-ruby-0.13.1 lib/code/object/boolean.rb
code-ruby-0.13.0 lib/code/object/boolean.rb