Patch for Struct::new documentation
"Paul Butcher" <[email protected]> Mon, 3 Sep 2007 21:20:48 +0100
| Newsgroups | gmane.comp.lang.ruby.documentation |
|---|---|
| Message-ID | <004101c7ee67$eaf845d0$7301a8c0@paullaptop> |
All, I've attached a patch for the documentation for Struct::new. The second edition of The Pickaxe Book says: > Ruby 1.9 and later allow you to pass a block to a Struct's constructor. > This block is evaluated in the context of the new struct's class and hence > allows you conveniently to add instance methods to the new struct. It turns out that this functionality has actually been present since 1.8.3! This is the relevant ChangeLog entry: http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/struct.c?view=log#rev5934 (although it references [ruby-talk:02606], it should really be [ruby-core:02606]: http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-core/2606 I've tried to stick to the submission guidelines, but please let me know if I've screwed anything up! Cheers, ------------------------------------------------ Paul Butcher CTO Texperts Mobile: +44 (0) 7740 857648 Main: +44 (0) 1223 309080 Fax: +44(0) 1223 309082 Email: [email protected] MSN: [email protected] AIM: paulrabutcher Skype: paulrabutcher LinkedIn: http://www.linkedin.com/in/paulbutcher ------------------------------------------------ Re5ult Limited Registered in England No 04909795 VAT registration number GB 849 201 231. Registered office 22 Great James Street Londong WC1N 3ES This email is confidential. It may be read, copied and used only by the intended recipient. If you have received it in error, please contact us immediately.
rdoc_changes_to_struct.patch
(application/octet-stream, 1.6 KB)
Index: struct.c
===================================================================
--- struct.c (revision 13340)
+++ struct.c (working copy)
@@ -237,9 +237,10 @@
/*
* call-seq:
- * Struct.new( [aString] [, aSym]+> ) => StructClass
- * StructClass.new(arg, ...) => obj
- * StructClass[arg, ...] => obj
+ * Struct.new( [aString] [, aSym]+> ) => StructClass
+ * Struct.new( [aString] [, aSym]+> ) {|| block} => StructClass
+ * StructClass.new(arg, ...) => obj
+ * StructClass[arg, ...] => obj
*
* Creates a new class, named by <i>aString</i>, containing accessor
* methods for the given symbols. If the name <i>aString</i> is
@@ -257,6 +258,9 @@
* class; unset parameters default to \nil{}. Passing too many
* parameters will raise an \E{ArgumentError}.
*
+ * If a block is given it's executed in the context of the new class,
+ * allowing methods to be added to the generated class conveniently.
+ *
* The remaining methods listed in this section (class and instance)
* are defined for this generated class.
*
@@ -267,6 +271,15 @@
* # Create a structure named by its constant
* Customer = Struct.new(:name, :address) #=> Customer
* Customer.new("Dave", "123 Main") #=> #<Customer name="Dave", address="123 Main">
+ *
+ * # Add a method to the generated class
+ * Customer = Struct.new(:name, :address) do
+ * def to_s
+ * ...
+ * end
+ * end
+ * c = Customer.new("Dave", "123 Main")
+ * c.to_s
*/
static VALUE