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



132
133
134
135
136
137
138
139
140
141
142
# File 'ext/RMagick/rmenum.c', line 132

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



177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'ext/RMagick/rmenum.c', line 177

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

    if(CLASS_OF(self) != CLASS_OF(other)) {
        return Qnil;
    }

    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);
    }

    return INT2FIX(0);
}

#===(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



105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'ext/RMagick/rmenum.c', line 105

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



154
155
156
157
158
159
160
161
# File 'ext/RMagick/rmenum.c', line 154

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



211
212
213
214
215
216
217
218
# File 'ext/RMagick/rmenum.c', line 211

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));
}