Class: Object

Inherits:
BasicObject
Defined in:
lib/utilrb/object/address.rb,
lib/utilrb/enumerable/uniq.rb,
lib/utilrb/object/attribute.rb,
lib/utilrb/object/attribute.rb,
lib/utilrb/object/scoped_eval.rb

Class Method Summary collapse

Instance Method Summary collapse

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

Instance Method Details

#addressObject

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

#attribute(attr_def, &init) ⇒ Object

call-seq:

attribute :name => default_value
attribute(:name) { default_value }

In the first form, defines a read-write attribute named 'name' with default_value for default value. In the second form, the block is called if the attribute is read before it has been ever written, and its return value is used as default value.


11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/utilrb/object/attribute.rb', line 11

def attribute(attr_def, &init) # :nodoc:
    if Hash === attr_def
        name, defval = attr_def.to_a.flatten
    else
        name = attr_def
    end

    class_eval do
        attr_writer name
        if !defval && init
            define_method("#{name}_defval", &init)
        else
            define_method("#{name}_defval") { defval }
        end
    end

    class_eval <<-EOD, __FILE__, __LINE__+1
    def #{name}
        if instance_variable_defined?(:@#{name}) then @#{name}
        elsif frozen?
            #{name}_defval
        else @#{name} = #{name}_defval
        end
    end
    EOD
end

#class_attribute(attr_def, &init) ⇒ Object

Like #attribute, but on the singleton class of this object


41
42
43
# File 'lib/utilrb/object/attribute.rb', line 41

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)


46
47
48
# File 'lib/utilrb/enumerable/uniq.rb', line 46

def enum_uniq(enum_with = :each, *args, &filter)
	UniqEnumerator.new(self, enum_with, args, filter)
end

#scoped_eval(type = :instance_eval, &b) ⇒ Object


4
5
6
# File 'lib/utilrb/object/scoped_eval.rb', line 4

def scoped_eval(type = :instance_eval, &b)
    send(type, &b)
end