class Friend attr_accessor :name, :age, :phone def initialize(name, age, phone) @name, @age, @phone = name, age, phone end end f1 = Friend.new("John", 30, "555-1212") p f1.instance f1.instance.update({:name=>'Jerry'}) p f1.instance
# File lib/core/facets/kernel/instance.rb, line 91 91: def <<(pair) 92: name, value = *pair 93: name = atize(name) 94: @delegate.instance_variable_set(name, value) 95: end
# File lib/core/facets/kernel/instance.rb, line 79 79: def [](name) 80: name = atize(name) 81: @delegate.instance_variable_get(name) 82: end
# File lib/core/facets/kernel/instance.rb, line 85 85: def []=(name, value) 86: name = atize(name) 87: @delegate.instance_variable_set(name,value) 88: end
# File lib/core/facets/kernel/instance.rb, line 43 43: def each 44: @delegate.instance_variables.each do |name| 45: yield(name[1..1].to_sym, @delegate.instance_variable_get(name)) 46: end 47: end
Instance evaluation.
# File lib/core/facets/kernel/instance.rb, line 145 145: def eval(*a,&b) 146: @delegate.instance_eval(*a,&b) 147: end
# File lib/core/facets/kernel/instance.rb, line 38 38: def instance_delegate 39: @delegate 40: end
Instance vairable names as symbols.
# File lib/core/facets/kernel/instance.rb, line 124 124: def keys 125: @delegate.instance_variables.collect do |name| 126: name[1..1].to_sym 127: end 128: end
Instance variable names as strings.
# File lib/core/facets/kernel/instance.rb, line 131 131: def names 132: @delegate.instance_variables.collect do |name| 133: name[1..1] 134: end 135: end
Return instance variables with values as a hash.
class X def initialize(a,b) @a, @b = a, b end end x = X.new(1,2) x.instance.to_h #=> { :a=>1, :b=>2 }
# File lib/core/facets/kernel/instance.rb, line 61 61: def to_h(at=false) 62: h = {} 63: if at 64: @delegate.instance_variables.each do |name| 65: h[name] = @delegate.instance_variable_get(name) 66: end 67: else 68: each do |key, value| 69: h[key] = value 70: end 71: end 72: h 73: end
Set instance variables given a hash.
instance.update('@a'=>1, '@b'=>2) @a #=> 1 @b #=> 2
Also, +@+ sign is not neccessary.
instance.update(:a=>1, :b=>2) @a #=> 1 @b #=> 2
# File lib/core/facets/kernel/instance.rb, line 109 109: def update(hash) 110: hash.each do |pair| 111: self << pair 112: end 113: end
Instance variable values.
# File lib/core/facets/kernel/instance.rb, line 138 138: def values 139: @delegate.instance_variables.collect do |name| 140: @delegate.instance_variable_get(name) 141: end 142: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.