# class DBus::Data::Variant

A generic type.

Implementation note: @value is a {Data::Base}.

### Attributes

member_type[R]

@return [Type]

### Public Class Methods

alignment() click to toggle source

        # File lib/dbus/data.rb
    717 def self.alignment
    718   1
    719 end

from_items(value, mode:, member_type:) click to toggle source

@param `member_type` [Type]

        # File lib/dbus/data.rb
    726 def self.from_items(value, mode:, member_type:)
    727   return value if mode == :plain
    728
    729   new(value, member_type: member_type)
    730 end

from_typed(value, type:) click to toggle source

@param value [::Object] @param type [Type] @return [Variant]

        # File lib/dbus/data.rb
    735 def self.from_typed(value, type:)
    736   assert_type_matches_class(type)
    737
    738   # nil: decide on type of value
    739   new(value, member_type: nil)
    740 end

guess_type(value) click to toggle source

Determine the type of value @param value [::Object] @return [Type] @api private See also {PacketMarshaller.make_variant}

        # File lib/dbus/data.rb
    763 def self.guess_type(value)
    764   sct, = PacketMarshaller.make_variant(value)
    765   DBus.type(sct)
    766 end

new(value, member_type:) click to toggle source

@param `member_type` [SingleCompleteType,Type,nil]

Calls superclass method `DBus::Data::Base::new`

        # File lib/dbus/data.rb
    769 def initialize(value, member_type:)
    770   member_type = Type::Factory.make_type(member_type) if member_type
    771   # TODO: validate that the given *member_type* matches *value*
    772   case value
    773   when Data::Variant
    774     # Copy the contained value instead of boxing it more
    775     # TODO: except perhaps for round-tripping in exact mode?
    776     @member_type = value.member_type
    777     value = value.exact_value
    778   when Data::Base
    779     @member_type = member_type || value.type
    780     raise ArgumentError, "Variant type #{@member_type} does not match value type #{value.type}" \
    781       unless @member_type == value.type
    782   else
    783     @member_type = member_type || self.class.guess_type(value)
    784     value = Data.make_typed(@member_type, value)
    785   end
    786   super(value)
    787 end

type() click to toggle source

@return [Type]

        # File lib/dbus/data.rb
    743 def self.type
    744   # memoize
    745   @type ||= Type.new(type_code).freeze
    746 end

type_code() click to toggle source

        # File lib/dbus/data.rb
    713 def self.type_code
    714   "v"
    715 end

### Public Instance Methods

[](index) click to toggle source

Internal helpers to keep the {DBus.variant} method working. Formerly it returned just a pair of [DBus.type(string_type), value] so let’s provide [0], [1], .first, .last

        # File lib/dbus/data.rb
    792 def [](index)
    793   case index
    794   when 0
    795     member_type
    796   when 1
    797     value
    798   else
    799     raise ArgumentError, "DBus.variant can only be indexed with 0 or 1, seen #{index.inspect}"
    800   end
    801 end

first() click to toggle source

@see `[]`

        # File lib/dbus/data.rb
    804 def first
    805   self[0]
    806 end

last() click to toggle source

@see `[]`

        # File lib/dbus/data.rb
    809 def last
    810   self[1]
    811 end

type() click to toggle source

Note that for Variants type.to_s==“v”, for the specific see {Variant#member_type} @return [Type] the exact type of this value

        # File lib/dbus/data.rb
    751 def type
    752   self.class.type
    753 end

value() click to toggle source

        # File lib/dbus/data.rb
    721 def value
    722   @value.value
    723 end