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



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

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) ⇒ -1, ...

Support Comparable module in Enum.

Parameters:

  • other (Object)

    the other object

Returns:

  • (-1, 0, 1, nil)

    the result of compare



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) ⇒ Boolean

“Case equal” operator for Enum.

Parameters:

  • other (Object)

    the other object

Returns:

  • (Boolean)

    true or false



120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'ext/RMagick/rmenum.c', line 120

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

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

    return Qfalse;
}

#to_iNumeric

Return the value of an enum.

Returns:

  • (Numeric)

    this object’s value



161
162
163
164
165
166
167
168
# File 'ext/RMagick/rmenum.c', line 161

VALUE
Enum_to_i(VALUE self)
{
    MagickEnum *magick_enum;

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

#to_sString

Return the name of an enum.

Returns:

  • (String)

    the name of an enum



236
237
238
239
240
# File 'ext/RMagick/rmenum.c', line 236

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

#|(another) ⇒ Magick::Enum

Bitwise OR for enums

Parameters:

Returns:



207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'ext/RMagick/rmenum.c', line 207

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

    Data_Get_Struct(self, MagickEnum, this);
    Data_Get_Struct(another, MagickEnum, that);
    Data_Get_Struct(new_enum, MagickEnum, 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;
}