From: Brian Candler Date: 2012-05-23T04:28:13+09:00 Subject: Re: Changing self class from inside a method?? > you could probably either start with an empty array instead of nil, or > "guard" all array methods with "ary ||= []". A related and useful idiom is auto-vivification of Hash elements: h = Hash.new { |h,k| h[k] = [] } h[:foo] << "bar" h[:foo] << "baz" p h # {:foo=>["bar", "baz"]} Objects in ruby cannot change their class. If you really need to do this, then you can have a proxy object which delegates calls, and you can change the target object dynamically: >> require 'delegate' => true >> myvar = SimpleDelegator.new(nil) => nil >> myvar.to_s => "" >> myvar.__setobj__([]) => [] >> myvar << "foo" << "bar" => ["foo", "bar"] -- Posted via http://www.ruby-forum.com/.