Class: Object
- Inherits:
- BasicObject
- Defined in:
- lib/utilrb/object/address.rb,
lib/utilrb/enumerable/uniq.rb,
lib/utilrb/object/attribute.rb,
lib/utilrb/object/scoped_eval.rb,
lib/utilrb/object/singleton_class.rb
Class Method Summary collapse
-
.address_from_id(id) ⇒ Object
Converts the object_id of a non-immediate object to its memory address.
- .immediate?(object) ⇒ Boolean
-
.swap!(obj1, obj2) ⇒ Object
Kernel.swap!(obj1, obj2, *args).
Instance Method Summary collapse
-
#address ⇒ Object
Return the object address (for non immediate objects).
-
#class_attribute(attr_def, &init) ⇒ Object
Like #attribute, but on the singleton class of this object.
-
#enum_uniq(enum_with = :each, *args, &filter) ⇒ Object
Enumerate using the
each(*args)
method, removing the duplicate entries. -
#is_singleton?(object) ⇒ Boolean
Returns true if
self
is a singleton class. - #scoped_eval(type = :instance_eval, &b) ⇒ Object
-
#singleton_class ⇒ Object
Returns the singleton class for this object.
Class Method Details
.address_from_id(id) ⇒ Object
Converts the object_id of a non-immediate object to its memory address
9 10 11 12 |
# File 'lib/utilrb/object/address.rb', line 9 def self.address_from_id(id) id = 0xFFFFFFFFFFFFFFFF - ~id if id < 0 (id * 2) & 0xFFFFFFFFFFFFFFFF end |
.immediate?(object) ⇒ Boolean
108 109 |
# File 'ext/utilrb_ext.cc', line 108 static VALUE kernel_is_immediate(VALUE klass, VALUE object) { return IMMEDIATE_P(object) ? Qtrue : Qfalse; } |
.swap!(obj1, obj2) ⇒ Object
Kernel.swap!(obj1, obj2, *args)
Swaps the object which are being hold by obj1 and obj2.
WARNING: I don’t know if this can be called in a method of obj1
or obj2
14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'ext/swap.cc', line 14 static VALUE kernel_swap_bang(VALUE self, VALUE obj1, VALUE obj2) { // Save the definition of the old object RVALUE old_obj; memcpy(&old_obj, reinterpret_cast<void*>(obj1), SLOT_SIZE); // Place the definition of the new object in the slot of the old one memcpy(reinterpret_cast<void*>(obj1), reinterpret_cast<void*>(obj2), SLOT_SIZE); // Place the definition of the old object in the slot of the new one memcpy(reinterpret_cast<void*>(obj2), &old_obj, SLOT_SIZE); return Qnil; } |
Instance Method Details
#address ⇒ Object
Return the object address (for non immediate objects).
5 |
# File 'lib/utilrb/object/address.rb', line 5 def address; Object.address_from_id(object_id) end |
#class_attribute(attr_def, &init) ⇒ Object
Like #attribute, but on the singleton class of this object
71 72 73 |
# File 'lib/utilrb/object/attribute.rb', line 71 def class_attribute(attr_def, &init) singleton_class.class_eval { attribute(attr_def, &init) } end |
#enum_uniq(enum_with = :each, *args, &filter) ⇒ Object
Enumerate using the each(*args)
method, removing the duplicate entries. If filter
is given, it should return an object the enumerator will compare for equality (instead of using the objects themselves)
52 53 54 |
# File 'lib/utilrb/enumerable/uniq.rb', line 52 def enum_uniq(enum_with = :each, *args, &filter) UniqEnumerator.new(self, enum_with, args, filter) end |
#is_singleton?(object) ⇒ Boolean
Returns true if self
is a singleton class
44 45 46 47 48 49 50 |
# File 'ext/utilrb_ext.cc', line 44 static VALUE kernel_is_singleton_p(VALUE self) { if (BUILTIN_TYPE(self) == T_CLASS && FL_TEST(self, FL_SINGLETON)) return Qtrue; else return Qfalse; } |
#scoped_eval(type = :instance_eval, &b) ⇒ Object
5 6 7 8 9 10 |
# File 'lib/utilrb/object/scoped_eval.rb', line 5 def scoped_eval(type = :instance_eval, &b) modules = b.binding.eval "Module.nesting" with_module(*modules) do send(type, &b) end end |
#singleton_class ⇒ Object
Returns the singleton class for this object.
In Ruby 1.8, makes sure that the #superclass method of the singleton class returns the object’s class (instead of Class), as Ruby 1.9 does
The first element of #ancestors on the returned singleton class is the singleton class itself. A #singleton_instance accessor is also defined, which returns the object instance the class is the singleton of.
15 16 17 |
# File 'lib/utilrb/object/singleton_class.rb', line 15 def singleton_class class << self; self end end |