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) ⇒ Magick::Enum

Initialize a new Enum instance.

Parameters:

  • sym (Symbol)

    the symbol as enum name

  • val (Numeric)

    the value for enum



162
163
164
165
166
167
168
169
170
171
172
# File 'ext/RMagick/rmenum.c', line 162

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

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

    return self;
}

Instance Method Details

#<=>(other) ⇒ -1, ...

Support Comparable module in Enum.

Parameters:

  • other (Object)

    the other object

Returns:

  • (-1, 0, 1, nil)

    the result of compare



196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'ext/RMagick/rmenum.c', line 196

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

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

    TypedData_Get_Struct(self, MagickEnum, &rm_enum_data_type, this);
    TypedData_Get_Struct(other, MagickEnum, &rm_enum_data_type, that);

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

    return INT2FIX(0);
}

#===(other) ⇒ Boolean

“Case equal” operator for Enum.

Parameters:

  • other (Object)

    the other object

Returns:

  • (Boolean)

    true or false



139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'ext/RMagick/rmenum.c', line 139

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

        TypedData_Get_Struct(self, MagickEnum, &rm_enum_data_type, this);
        TypedData_Get_Struct(other, MagickEnum, &rm_enum_data_type, that);
        return this->val == that->val ? Qtrue : Qfalse;
    }

    return Qfalse;
}

#to_iNumeric

Return the value of an enum.

Returns:

  • (Numeric)

    this object’s value



180
181
182
183
184
185
186
187
# File 'ext/RMagick/rmenum.c', line 180

VALUE
Enum_to_i(VALUE self)
{
    MagickEnum *magick_enum;

    TypedData_Get_Struct(self, MagickEnum, &rm_enum_data_type, magick_enum);
    return INT2NUM(magick_enum->val);
}

#to_sString

Return the name of an enum.

Returns:

  • (String)

    the name of an enum



255
256
257
258
259
# File 'ext/RMagick/rmenum.c', line 255

VALUE
Enum_to_s(VALUE self)
{
    return rb_str_new2(rm_enum_to_cstr(self));
}

#|(another) ⇒ Magick::Enum

Bitwise OR for enums

Parameters:

Returns:



226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'ext/RMagick/rmenum.c', line 226

VALUE
Enum_bitwise_or(VALUE self, VALUE another)
{
    VALUE new_enum, cls;
    MagickEnum *this, *that, *new_enum_data;

    cls = CLASS_OF(self);
    if (CLASS_OF(another) != cls)
    {
        rb_raise(rb_eArgError, "Expected class %s but got %s", rb_class2name(cls), rb_class2name(CLASS_OF(another)));
    }

    new_enum = Enum_alloc(cls);

    TypedData_Get_Struct(self, MagickEnum, &rm_enum_data_type, this);
    TypedData_Get_Struct(another, MagickEnum, &rm_enum_data_type, that);
    TypedData_Get_Struct(new_enum, MagickEnum, &rm_enum_data_type, new_enum_data);

    new_enum_data->id = rb_to_id(rb_sprintf("%s|%s", rb_id2name(this->id), rb_id2name(that->id)));
    new_enum_data->val = this->val | that->val;

    return new_enum;
}