Class: Magick::Enum

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
ext/RMagick/rmmain.c

Direct Known Subclasses

GeometryValue

Instance Method Summary collapse

Constructor Details

#initialize(sym, val) ⇒ Object

Initialize a new Enum instance.

Ruby usage:

- @verbatim Enum#initialize(sym,val) @endverbatim

Parameters:

  • self

    this object

  • sym

    the symbol

  • val

    the value for the symbol



129
130
131
132
133
134
135
136
137
138
139
# File 'ext/RMagick/rmenum.c', line 129

VALUE
Enum_initialize(VALUE self, VALUE sym, VALUE val)
{
   MagickEnum *magick_enum;

   Data_Get_Struct(self, MagickEnum, magick_enum);
   magick_enum->id = rb_to_id(sym); /* convert symbol to ID */
   magick_enum->val = NUM2INT(val);

   return self;
}

Instance Method Details

#<=>(other) ⇒ Object

Support Comparable module in Enum.

Ruby usage:

- @verbatim Enum#<=> @endverbatim

Notes:

- Enums must be instances of the same class to be equal.

Parameters:

  • self

    this object

  • other

    the other object

Returns:

  • -1, 0, 1, or nil



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'ext/RMagick/rmenum.c', line 174

VALUE
Enum_spaceship(VALUE self, VALUE other)
{
    MagickEnum *this, *that;

    Data_Get_Struct(self, MagickEnum, this);
    Data_Get_Struct(other, MagickEnum, that);

    if (this->val > that->val)
    {
        return INT2FIX(1);
    }
    else if (this->val < that->val)
    {
        return INT2FIX(-1);
    }

    // Values are equal, check class.

    return rb_funcall(CLASS_OF(self), rm_ID_spaceship, 1, CLASS_OF(other));
}

#===(other) ⇒ Object

“Case equal” operator for Enum.

Ruby usage:

- @verbatim Enum#=== @endverbatim

Notes:

- Yes, I know "case equal" is a misnomer.

Parameters:

  • self

    this object

  • other

    the other object

Returns:

  • true or false



102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'ext/RMagick/rmenum.c', line 102

VALUE
Enum_case_eq(VALUE self, VALUE other)
{
    MagickEnum *this, *that;

    if (CLASS_OF(self) == CLASS_OF(other))
    {
        Data_Get_Struct(self, MagickEnum, this);
        Data_Get_Struct(other, MagickEnum, that);
        return this->val == that->val ? Qtrue : Qfalse;
    }

    return Qfalse;
}

#to_iObject

Return the value of an enum.

Ruby usage:

- @verbatim Enum#to_i @endverbatim

Parameters:

  • self

    this object

Returns:

  • this object’s value



151
152
153
154
155
156
157
158
# File 'ext/RMagick/rmenum.c', line 151

VALUE
Enum_to_i(VALUE self)
{
   MagickEnum *magick_enum;

   Data_Get_Struct(self, MagickEnum, magick_enum);
   return INT2NUM(magick_enum->val);
}

#to_sObject

Return the name of an enum.

Ruby usage:

- @verbatim Enum#to_s @endverbatim

Parameters:

  • self

    this object

Returns:

  • the name



206
207
208
209
210
211
212
213
# File 'ext/RMagick/rmenum.c', line 206

VALUE
Enum_to_s(VALUE self)
{
   MagickEnum *magick_enum;

   Data_Get_Struct(self, MagickEnum, magick_enum);
   return rb_str_new2(rb_id2name(magick_enum->id));
}