Method: Object#clone
- Defined in:
- lib/source/ruby.rb
#clone ⇒ Object
call-seq:
obj.clone -> object
Produces a shallow copy of obj – the instance variables of obj are copied, but not the objects they reference. See also the discussion under Object#dup.
class Klass
attr_accessor :str
end
s1 = Klass.new #=> #<Klass:100>
s1.str = "Hello" #=> "Hello"
s2 = s1.clone #=> #<Klass:101>
s2.str[1,4] = "i" #=> "i"
s1.str #=> "Hi"
s2.str #=> "Hi"
This method may have class-specific behavior. If so, that behavior will be documented under the initialize_copy method of the class.
273 274 275 276 277 278 |
# File 'lib/source/ruby.rb', line 273 def clone `var result={}` `for(var x in this){if(x!='__id__'){result[x]=this[x];};}` `result.__id__=Red.id++` return `result` end |