Class: Kernel::Mirror

Inherits:
Object show all
Includes:
Enumerable
Defined in:
lib/standard/facets/mirror.rb

Overview

A Mirror looks at an object from the outside: its instance variables, its class and its methods. Ruby puts these reflection methods on every object, where a class can redefine them or, like BasicObject, not have them. A mirror calls Ruby's own versions, saved when this file loads, so it gives the true answers for any object.

class Song
  def initialize(title, year)
    @title, @year = title, year
  end
end

song = Song.new("Paranoid", 1970)
mirror = song.mirror        # or Mirror.new(song)

mirror.keys                 #=> [:title, :year]
mirror[:title]              #=> "Paranoid"
mirror.to_h                 #=> {title: "Paranoid", year: 1970}
mirror.update(year: 1971)
mirror[:year]               #=> 1971

Instance variable names can be given with or without the "@".

The class lives in Kernel, so Mirror works everywhere, but an application's own top-level Mirror class doesn't clash with it: the application's takes precedence, and this one is still Kernel::Mirror. A class can also define its own #mirror, so code that must work on any object should use Mirror.new(obj).

NOTE: This is not loaded by require 'facets'. Use:

require 'facets/mirror'

Based on the Instance gem.

Since: 4.2.0 Author: Trans

Constant Summary

Constants included from Enumerable

Enumerable::Arguments

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Enumerable

#accumulate, #accumulate_all, #apply, #cluster, #compact_map, #defer, #each_by, #every, #every!, #ewise, #exclude?, #expand, #find_yield, #frequency, #graph, #hashify, #hinge, #incase?, #indexes, #key_by, #map_by, #map_send, #map_to, #map_with, #map_with_index, #mash, #modulate, #occur, #only, #only?, #organize_by, #pair, #per, #purge, #recursively, #squeeze, #threaded_map, #threaded_map_send, #unassociate, #uniq_by, #value_by, #visit, #with_position

Constructor Details

#initialize(object) ⇒ Mirror

A mirror of object.



68
69
70
# File 'lib/standard/facets/mirror.rb', line 68

def initialize(object)
  @object = object
end

Instance Attribute Details

#object ⇒ Object (readonly)

The object this mirror reflects.



73
74
75
# File 'lib/standard/facets/mirror.rb', line 73

def object
  @object
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?

Two mirrors are equal if they reflect the same object.



225
226
227
# File 'lib/standard/facets/mirror.rb', line 225

def ==(other)
  other.is_a?(Mirror) && other.object.equal?(@object)
end

#each ⇒ Object

Yields each instance variable's name, without the "@", and value. Mirror is Enumerable, so to_h, select and the rest work too.

mirror.to_h  #=> {title: "Paranoid", year: 1970}


103
104
105
106
107
108
109
# File 'lib/standard/facets/mirror.rb', line 103

def each
  return to_enum(:each) { size } unless block_given?
  variables.each do |name|
    yield name[1..-1].to_sym, call(:instance_variable_get, name)
  end
  self
end

#eval(*args, &block) ⇒ Object

Evaluates a string or a block with the object as self, like instance_eval.



145
146
147
# File 'lib/standard/facets/mirror.rb', line 145

def eval(*args, &block)
  call(:instance_eval, *args, &block)
end

#exec(*args, &block) ⇒ Object

Calls the block with the object as self, passing the arguments, like instance_exec.



151
152
153
# File 'lib/standard/facets/mirror.rb', line 151

def exec(*args, &block)
  call(:instance_exec, *args, &block)
end

#get(name) ⇒ Object Also known as: []

Value of an instance variable, or nil if it isn't set.



112
113
114
# File 'lib/standard/facets/mirror.rb', line 112

def get(name)
  call(:instance_variable_get, ivar(name))
end

#hash ⇒ Object



230
231
232
# File 'lib/standard/facets/mirror.rb', line 230

def hash
  [Mirror, id].hash
end

#id ⇒ Object

The object's object_id.



172
173
174
# File 'lib/standard/facets/mirror.rb', line 172

def id
  call(:object_id)
end

#inspect ⇒ Object

Shows the object's class and address, without calling the object's own #inspect, which it may not have.



236
237
238
# File 'lib/standard/facets/mirror.rb', line 236

def inspect
  "#<#{self.class} of #{call(:to_s)}>"
end

#is?(mod) ⇒ Boolean

Is the object an instance of this class or module, or of a subclass, as with is_a??

Returns:

  • (Boolean)


167
168
169
# File 'lib/standard/facets/mirror.rb', line 167

def is?(mod)
  call(:kind_of?, mod)
end

#key?(name) ⇒ Boolean

Is the instance variable set?

Returns:

  • (Boolean)


133
134
135
# File 'lib/standard/facets/mirror.rb', line 133

def key?(name)
  call(:instance_variable_defined?, ivar(name))
end

#keys ⇒ Object

Instance variable names as symbols, without the "@".

mirror.keys  #=> [:title, :year]


85
86
87
# File 'lib/standard/facets/mirror.rb', line 85

def keys
  variables.map { |name| name[1..-1].to_sym }
end

#method_definition(name) ⇒ Object

For a module or class: the instance method of this name, as an UnboundMethod. Raises TypeError for any other object.



220
221
222
# File 'lib/standard/facets/mirror.rb', line 220

def method_definition(name)
  module_call(:instance_method, name)
end

#method_definitions(*kinds) ⇒ Object

For a module or class: names of the methods it defines for its instances. With no arguments, the same as instance_methods. Otherwise any of :public, :protected and :private, or :all. Raises TypeError for any other object.



205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/standard/facets/mirror.rb', line 205

def method_definitions(*kinds)
  return module_call(:instance_methods) if kinds.empty?
  kinds = [:public, :protected, :private] if kinds.include?(:all)
  kinds.uniq.flat_map do |kind|
    case kind
    when :public    then module_call(:public_instance_methods)
    when :protected then module_call(:protected_instance_methods)
    when :private   then module_call(:private_instance_methods)
    else raise ArgumentError, "unknown kind of method: #{kind.inspect}"
    end
  end
end

#method_names(*kinds) ⇒ Object

Names of the object's methods. With no arguments, the same as methods. Otherwise any of :public, :protected, :private and :singleton, or :all for the first three.

mirror.method_names(:private)


181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/standard/facets/mirror.rb', line 181

def method_names(*kinds)
  return call(:methods) if kinds.empty?
  kinds = [:public, :protected, :private] if kinds.include?(:all)
  kinds.uniq.flat_map do |kind|
    case kind
    when :public    then call(:public_methods)
    when :protected then call(:protected_methods)
    when :private   then call(:private_methods)
    when :singleton then call(:singleton_methods)
    else raise ArgumentError, "unknown kind of method: #{kind.inspect}"
    end
  end
end

#method_object(name) ⇒ Object

The object's method of this name, as a Method, even if the object redefines #method.



197
198
199
# File 'lib/standard/facets/mirror.rb', line 197

def method_object(name)
  call(:method, name)
end

#object_class ⇒ Object

The object's class, even if the object redefines #class.



156
157
158
# File 'lib/standard/facets/mirror.rb', line 156

def object_class
  call(:class)
end

#of?(klass) ⇒ Boolean

Is the object an instance of exactly this class?

Returns:

  • (Boolean)


161
162
163
# File 'lib/standard/facets/mirror.rb', line 161

def of?(klass)
  call(:instance_of?, klass)
end

#remove(name) ⇒ Object

Removes an instance variable, and returns its value. Raises NameError if it isn't set.



139
140
141
# File 'lib/standard/facets/mirror.rb', line 139

def remove(name)
  call(:remove_instance_variable, ivar(name))
end

#set(name, value) ⇒ Object Also known as: []=

Sets an instance variable, and returns the value.



118
119
120
# File 'lib/standard/facets/mirror.rb', line 118

def set(name, value)
  call(:instance_variable_set, ivar(name), value)
end

#size ⇒ Object

Number of instance variables.



95
96
97
# File 'lib/standard/facets/mirror.rb', line 95

def size
  variables.size
end

#update(hash) ⇒ Object

Sets instance variables from a hash of names and values, and returns the mirror.

mirror.update(title: "War Pigs", year: 1970)


127
128
129
130
# File 'lib/standard/facets/mirror.rb', line 127

def update(hash)
  hash.each { |name, value| set(name, value) }
  self
end

#values ⇒ Object

Instance variable values, in the same order as #keys.



90
91
92
# File 'lib/standard/facets/mirror.rb', line 90

def values
  variables.map { |name| call(:instance_variable_get, name) }
end

#variables ⇒ Object

Instance variable names as Ruby gives them, with the "@".

mirror.variables  #=> [:@title, :@year]


78
79
80
# File 'lib/standard/facets/mirror.rb', line 78

def variables
  call(:instance_variables)
end