Class: Instance
- Includes:
- Enumerable
- Defined in:
- lib/core/facets/instance.rb
Overview
Instance Class
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")
f1.instance
f1.instance.update({:name=>'Jerry'})
f1.instance
Instance Method Summary collapse
- #<<(pair) ⇒ Object
- #[](name) ⇒ Object
- #[]=(name, value) ⇒ Object
- #each ⇒ Object
-
#eval(*a, &b) ⇒ Object
Instance evaluation.
-
#initialize(delegate) ⇒ Instance
constructor
A new instance of Instance.
- #instance_delegate ⇒ Object
-
#keys ⇒ Object
Instance vairable names as symbols.
-
#names ⇒ Object
Instance variable names as strings.
-
#to_h(at = false) ⇒ Object
(also: #to_hash)
Return instance variables with values as a hash.
-
#update(hash) ⇒ Object
(also: #assign)
Set instance variables given a
hash
. -
#values ⇒ Object
Instance variable values.
-
#variables ⇒ Object
Same as #instance_variables.
Methods included from Enumerable
#accumulate, #accumulate_all, #cluster_by, #compact_map, #count, #defer, #each_by, #each_with_object, #every, #every!, #ewise, #exclude?, #expand, #filter, #find_yield, #frequency, #graph, #group_by, #has?, #map_by, #map_send, #map_with, #map_with_index, #modulate, #none?, #occur, #one?, #per, #purge, #recursively, #sum, #take, #threaded_map, #threaded_map_send, #to_h_assoc, #to_h_auto, #to_h_flat, #to_h_multi, #to_h_splat, #uniq_by, #visit
Constructor Details
#initialize(delegate) ⇒ Instance
Returns a new instance of Instance.
34 35 36 |
# File 'lib/core/facets/instance.rb', line 34 def initialize(delegate) @delegate = delegate end |
Instance Method Details
#<<(pair) ⇒ Object
92 93 94 95 96 |
# File 'lib/core/facets/instance.rb', line 92 def <<(pair) name, value = *pair name = atize(name) @delegate.instance_variable_set(name, value) end |
#[](name) ⇒ Object
80 81 82 83 |
# File 'lib/core/facets/instance.rb', line 80 def [](name) name = atize(name) @delegate.instance_variable_get(name) end |
#[]=(name, value) ⇒ Object
86 87 88 89 |
# File 'lib/core/facets/instance.rb', line 86 def []=(name, value) name = atize(name) @delegate.instance_variable_set(name,value) end |
#each ⇒ Object
44 45 46 47 48 |
# File 'lib/core/facets/instance.rb', line 44 def each @delegate.instance_variables.each do |name| yield(name[1..-1].to_sym, @delegate.instance_variable_get(name)) end end |
#eval(*a, &b) ⇒ Object
Instance evaluation.
146 147 148 |
# File 'lib/core/facets/instance.rb', line 146 def eval(*a,&b) @delegate.instance_eval(*a,&b) end |
#instance_delegate ⇒ Object
39 40 41 |
# File 'lib/core/facets/instance.rb', line 39 def instance_delegate @delegate end |
#keys ⇒ Object
Instance vairable names as symbols.
125 126 127 128 129 |
# File 'lib/core/facets/instance.rb', line 125 def keys @delegate.instance_variables.collect do |name| name[1..-1].to_sym end end |
#names ⇒ Object
Instance variable names as strings.
132 133 134 135 136 |
# File 'lib/core/facets/instance.rb', line 132 def names @delegate.instance_variables.collect do |name| name[1..-1] end end |
#to_h(at = false) ⇒ Object Also known as: to_hash
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 }
62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/core/facets/instance.rb', line 62 def to_h(at=false) h = {} if at @delegate.instance_variables.each do |name| h[name] = @delegate.instance_variable_get(name) end else each do |key, value| h[key] = value end end h end |
#update(hash) ⇒ Object Also known as: assign
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
110 111 112 113 114 |
# File 'lib/core/facets/instance.rb', line 110 def update(hash) hash.each do |pair| self << pair end end |
#values ⇒ Object
Instance variable values.
139 140 141 142 143 |
# File 'lib/core/facets/instance.rb', line 139 def values @delegate.instance_variables.collect do |name| @delegate.instance_variable_get(name) end end |
#variables ⇒ Object
Same as #instance_variables.
120 121 122 |
# File 'lib/core/facets/instance.rb', line 120 def variables @delegate.instance_variables end |