struct Rubellite::Value

Overview

Represents a first-class Ruby value (equivalent to Ruby's C VALUE). Provides transparent type checks, unboxing to Crystal primitives, dynamic method dispatch, operator overloads, and block execution.

Defined in:

rubellite/convert.cr
rubellite/typed_data.cr
rubellite/value.cr

Constructors

Instance Method Summary

Constructor Detail

def self.from(obj) : Value #

Converts a Crystal object to a Rubellite::Value


[View source]
def self.new(raw : LibRuby::Value) #

[View source]

Instance Method Detail

def *(other) : Value #

[View source]
def +(other) : Value #

[View source]
def -(other) : Value #

[View source]
def /(other) : Value #

[View source]
def <(other) : Bool #

[View source]
def <=(other) : Bool #

[View source]
def ==(other : Value) : Bool #

[View source]
def ==(other) : Bool #
Description copied from struct Struct

Returns true if this struct is equal to other.

Both structs' instance vars are compared to each other. Thus, two structs are considered equal if each of their instance variables are equal. Subclasses should override this method to provide specific equality semantics.

struct Point
  def initialize(@x : Int32, @y : Int32)
  end
end

p1 = Point.new 1, 2
p2 = Point.new 1, 2
p3 = Point.new 3, 4

p1 == p2 # => true
p1 == p3 # => false

[View source]
def >(other) : Bool #

[View source]
def >=(other) : Bool #

[View source]
def [](key) : Value #

[View source]
def []=(key, val) : Value #

[View source]
def array? : Bool #

[View source]
def as_bool : Bool #

[View source]
def as_crystal #

Auto-converts to Crystal primitive where possible


[View source]
def as_crystal_deep : DeepValue #

Recursive deep unboxer converting nested Arrays/Hashes into native Crystal collections


[View source]
def as_f : Float64 #

[View source]
def as_i : Int32 #

Convenience unboxers


[View source]
def as_i64 : Int64 #

[View source]
def as_nil : Nil #

[View source]
def as_s : String #

[View source]
def as_typed_data(type : T.class) : T forall T #

Unwraps this Ruby Value into a Crystal TypedData instance of type T. Raises TypeException if the Value does not wrap type T.


[View source]
def as_typed_data?(type : T.class) : T | Nil forall T #

Safely unwraps this Ruby Value into a Crystal TypedData instance, returning nil on type mismatch.


[View source]
def bool? : Bool #

[View source]
def call(method_name : String | Symbol, *args) : Value #

Invokes a method on the Ruby object with protected execution


[View source]
def call(method_name : String | Symbol, *args, &block : Array(Value) -> Value) : Value #

Invokes a method with a block, delegating to call_with_block


[View source]
def call?(method_name : String | Symbol, *args) : Value | Nil #

Safe method dispatch: returns nil if an exception or NoMethodError occurs


[View source]
def call_with_block(method_name : String | Symbol, *args, &block : Array(Value) -> Value) : Value #

Invokes a method while passing a Crystal block as a Ruby block with protected execution


[View source]
def call_with_kwargs(method_name : String | Symbol, *args, **kwargs) : Value #

Invokes a method with positional arguments and keyword arguments using Ruby 3+ kwargs protocol


[View source]
def class_name : String #

[View source]
def complex? : Bool #

[View source]
def dig(*keys) : Value | Nil #

Safe nested key navigation across Hashes and Arrays (transparently handles String and Symbol keys)


[View source]
def each(&block : Value -> Nil) : Nil #

Iterates over each element in a Ruby Enumerable / Array


[View source]
def each_pair(&block : Value, Value -> Nil) : Nil #

Iterates over key-value pairs in a Ruby Hash


[View source]
def empty? : Bool #

[View source]
def false? : Bool #

[View source]
def fixnum? : Bool #

[View source]
def float? : Bool #

[View source]
def flonum? : Bool #

[View source]
def has_key?(key) : Bool #

[View source]
def hash? : Bool #

[View source]
def inspect(io : IO) : Nil #
Description copied from struct Struct

Appends this struct's name and instance variables names and values to the given IO.

struct Point
  def initialize(@x : Int32, @y : Int32)
  end
end

p1 = Point.new 1, 2
p1.to_s    # "Point(@x=1, @y=2)"
p1.inspect # "Point(@x=1, @y=2)"

[View source]
def inspect : String #
Description copied from class Object

Returns an unambiguous and information-rich string representation of this object, typically intended for developers.

This method should usually not be overridden. It delegates to #inspect(IO) which can be overridden for custom implementations.

Also see #to_s.


[View source]
def integer? : Bool #

[View source]
def keys : Array(Value) #

[View source]
def kind_of?(class_name : String) : Bool #

Introspection: checks if the Ruby object is a kind of a Ruby class


[View source]
def length : Int32 #

[View source]
def numeric? : Bool #

[View source]
def proc? : Bool #

[View source]
def range? : Bool #

[View source]
def rational? : Bool #

[View source]
def raw : LibRuby::Value #

[View source]
def regex? : Bool #

[View source]
def respond_to?(method_name : String | Symbol) : Bool #

Introspection: checks if the Ruby object responds to a method


[View source]
def ruby_is_a?(class_name : String) : Bool #

[View source]
def ruby_nil? : Bool #

[View source]
def send(method_name : String | Symbol, *args) : Value #

Alias for call


[View source]
def set? : Bool #

[View source]
def size : Int32 #

[View source]
def string? : Bool #

[View source]
def symbol? : Bool #

[View source]
def symbol_name : String #

[View source]
def time? : Bool #

[View source]
def to_a(type : T.class) : Array(T) forall T #

Typed unboxing for Arrays


[View source]
def to_a : Array(Value) #

[View source]
def to_bool : Bool #

Ruby truthiness: false and nil are falsey, everything else is truthy


[View source]
def to_f : Float64 #

[View source]
def to_f64 : Float64 #

[View source]
def to_h(k_type : K.class, v_type : V.class) : Hash(K, V) forall K, V #

Typed unboxing for Hashes


[View source]
def to_h : Hash(Value, Value) #

[View source]
def to_i : Int32 #

[View source]
def to_i32 : Int32 #

[View source]
def to_i64 : Int64 #

[View source]
def to_range : Range(Int64, Int64) | Nil #

[View source]
def to_s(io : IO) : Nil #
Description copied from struct Struct

Same as #inspect(io).


[View source]
def to_s : String #
Description copied from class Object

Returns a nicely readable and concise string representation of this object, typically intended for users.

This method should usually not be overridden. It delegates to #to_s(IO) which can be overridden for custom implementations.

Also see #inspect.


[View source]
def to_set : Set(Value) #

[View source]
def to_slice : Bytes #

Converts Ruby string (including embedded null bytes) to a native Crystal byte slice


[View source]
def to_sym : String #

[View source]
def to_time : Time #

[View source]
def true? : Bool #

[View source]
def typed_data?(type : T.class) : Bool forall T #

Checks if this Ruby Value is a TypedData wrapping type T.


[View source]
def values : Array(Value) #

[View source]