Class: Godot::Variant

Inherits:
Object
  • Object
show all
Includes:
Godot
Defined in:
lib/godot_rb/variant.rb,
lib/godot_rb/enumerable.rb

Overview

Variant is the atomic type in GDScript; correspondingly,
the Godot::Variant Ruby class wraps Variant data and provides universal APIs for it and its descendant classes.
Methods use the implicit conversion #to_godot to convert your everyday Ruby objects to Godot::Variants.

Internally, Variants are gap-bridging wrappers of references of various data structures
(internal to Godot Engine) or (for the simplest data such as ints) immediate values.
As such, both Godot Engine and Ruby Variants are pint-sized, passed-by-value and expendable.
Ruby programmers should treat them similar to Rationals – unlike immediate values such as Integers,
two Variants may be distinct Ruby instances even if they reference the same Godot-internal data.

Direct Known Subclasses

Dictionary, Object, AbstractArray

Defined Under Namespace

Classes: AbstractArray

Constant Summary collapse

VARIANT_TYPE =

“one does not simply `initialize' a Variant”

-1 # “one does not simply `initialize' a Variant”

Constants included from Godot

VERSION

Instance Method Summary collapse

Methods included from Godot

init_level

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, **kwargs) ⇒ Object

Deprecated.


43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/godot_rb/variant.rb', line 43

def method_missing(name, *args, **kwargs)
# Zeroth, Ruby suffixes are special
  case name[-1]
  when '='
    public_send(:[]=, # https://github.com/soutaro/steep/issues/914
      name[..-1], #: ::String
      *args
    )
  when '?'
    godot_send("is_#{name[..-1]}", *args)
  else
    no_method_error = NoMethodError #: _Exception # https://github.com/soutaro/steep/issues/919
# First, check methods (if checking `attr_reader`s first, #[] can return a {Callable})
    begin
      return godot_send(name, *args) # `rescue` attaches `KeyError` cause if this raises
    rescue NoMethodError => no_method_error
      # fall-through, clear {$!} to prevent circular causes
    end
    # @ t ype var no_method_error NoMethodError[self]
    # Second, check `attr_reader`s
    if args.empty? # necessary condition for `attr_reader`
      begin
        return self[name]
      rescue => e # `attr_reader`s doesn’t work either: re-raise
        raise no_method_error, cause: e
      end
    else
      raise no_method_error
    end
  end
end

Instance Method Details

#getObject



16
17
18
19
20
# File 'lib/godot_rb/variant.rb', line 16

def get(...)
  self.[](...)
rescue KeyError => e
  warn "#{e.backtrace&.first}: #{e.detailed_message}"
end

#key?(key) ⇒ Boolean

Same as #has_key, but returns false instead of raising TypeError

Returns:

  • (Boolean)


28
29
30
31
32
# File 'lib/godot_rb/variant.rb', line 28

def key?(key)
  has_key(key)
rescue TypeError
  false
end

#respond_to_missing?(name, _include_all) ⇒ Boolean

Deprecated.

Returns:

  • (Boolean)


35
36
37
38
39
40
41
# File 'lib/godot_rb/variant.rb', line 35

def respond_to_missing?(name, _include_all) # https://github.com/soutaro/steep/issues/913
  case name[-1] # again, Ruby suffixes are special
  when '=' then key? name[..-1]
  when '?' then has_method "is_#{name[..-1]}"
  else has_method name or key? name
  end or super
end

#setObject



21
22
23
24
25
# File 'lib/godot_rb/variant.rb', line 21

def set(...)
  self.[]=(...)
rescue KeyError => e
  warn "#{e.backtrace&.first}: #{e.detailed_message}"
end

#to_godotObject



78
# File 'lib/godot_rb/variant.rb', line 78

def to_godot = self