This wasn't clear from the docs, or the example, but I tried it:
$ irb < ivarset.rb
>> class Fred
>> def initialize(p1, p2)
>> @a, @b = p1, p2
>> end
>> end
=> nil
>> fred = Fred.new('cat', 99)
=> #<Fred:0x2fd764 @a="cat", @b=99>
>> fred.instance_variable_set(:@a, 'dog')
=> "dog"
>> fred.instance_variable_set(:@c, 'cat')
=> "cat"
>> fred.inspect
=> "#<Fred:0x2fd764 @a=\"dog\", @b=99, @c=\"cat\">"
>>
Doc patch:
Index: object.c
===================================================================
RCS file: /src/ruby/object.c,v
retrieving revision 1.134.2.23
diff -u -r1.134.2.23 object.c
--- object.c 11 Jan 2005 15:36:52 -0000 1.134.2.23
+++ object.c 6 Feb 2005 22:14:26 -0000
@@ -2009,7 +2009,8 @@
*
* Sets the instance variable names by <i>symbol</i> to
* <i>object</i>, thereby frustrating the efforts of the class's
- * author to attempt to provide proper encapsulation.
+ * author to attempt to provide proper encapsulation. The variable
+ * did not have to exist prior to this call.
*
* class Fred
* def initialize(p1, p2)
@@ -2018,7 +2019,8 @@
* end
* fred = Fred.new('cat', 99)
* fred.instance_variable_set(:@a, 'dog') #=> "dog"
- * fred.inspect #=> "#<Fred:0x401b3da8
@a=\"dog\", @b=99>"
+ * fred.instance_variable_set(:@c, 'cat') #=> "cat"
+ * fred.inspect #=> "#<Fred:0x401b3da8
@a=\"dog\", @b=99, @c=\"cat\">"
*/
static VALUE
Thanks,
Sam
|