Class: Object
- Inherits:
- BasicObject
- Defined in:
- lib/ruby_ext.rb,
lib/more/typecast.rb
Instance Method Summary collapse
-
#cast_from(object) ⇒ Object
Cast on object from another.
-
#cast_to(klass) ⇒ Object
Cast an object to another.
-
#extend_once(*mods) ⇒ Object
Does what its name says.
-
#include_once(*mods) ⇒ Object
Does what its name says.
-
#instance_variables_send(obj, force = false) ⇒ Object
(also: #ivs_send)
Sets all instance variables of an object to another.
Instance Method Details
#cast_from(object) ⇒ Object
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/more/typecast.rb', line 121 def cast_from(object) method_to = "to_#{self.name.methodize}".to_sym if object.respond_to? method_to retval = object.send(method_to) return retval end method_from = "from_#{object.class.name.methodize}".to_sym if respond_to? method_from retval = send(method_from, object) return retval end raise TypeCastException, "TypeCasting from #{object.class.name} to #{self.name} not supported" end |
#cast_to(klass) ⇒ Object
Cast an object to another
1234.cast_to(String) => "1234"
113 114 115 |
# File 'lib/more/typecast.rb', line 113 def cast_to(klass) klass.cast_from(self) end |
#extend_once(*mods) ⇒ Object
Does what its name says
81 82 83 84 85 |
# File 'lib/ruby_ext.rb', line 81 def extend_once(*mods) mods.each do |mod| extend mod unless .ancestors.include? mod end end |
#include_once(*mods) ⇒ Object
Does what its name says
88 89 90 91 92 |
# File 'lib/ruby_ext.rb', line 88 def include_once(*mods) mods.each do |mod| include mod unless ancestors.include? mod end end |
#instance_variables_send(obj, force = false) ⇒ Object Also known as: ivs_send
Sets all instance variables of an object to another
if force is set to true, existing instance variables will be overwritten
70 71 72 73 74 75 76 77 |
# File 'lib/ruby_ext.rb', line 70 def instance_variables_send(obj, force=false) instance_variables.each do |v| if force or not obj.instance_variables.include? v obj.instance_variable_set(v, instance_variable_get(v)) end end obj end |