From 1eb9f71c3ae41182a08cd965340854f116354827 Mon Sep 17 00:00:00 2001 From: drbrain Date: Wed, 16 Jan 2013 23:36:46 +0000 Subject: * doc/syntax/miscellaneous.rdoc: Added documentation for alias, undef, BEGIN, END. * doc/syntax/modules_and_classes.rdoc (Constants): Fixed unwrapped paragraph with trailing whitespace. * doc/syntax/modules_and_classes.rdoc (Scope): Added section pointing to alias and undef documentation. * doc/syntax.rdoc: Added link to miscellaneous section. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@38853 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- doc/syntax/miscellaneous.rdoc | 61 +++++++++++++++++++++++++++++++++++++ doc/syntax/modules_and_classes.rdoc | 11 +++++-- 2 files changed, 70 insertions(+), 2 deletions(-) create mode 100644 doc/syntax/miscellaneous.rdoc (limited to 'doc/syntax') diff --git a/doc/syntax/miscellaneous.rdoc b/doc/syntax/miscellaneous.rdoc new file mode 100644 index 0000000000..409afc797b --- /dev/null +++ b/doc/syntax/miscellaneous.rdoc @@ -0,0 +1,61 @@ += Miscellaneous Syntax + +== +alias+ + +The +alias+ keyword is most frequently used to alias methods. When aliasing a +method you can use either its name or a symbol: + + alias new_name old_name + alias :new_name :old_name + +For methods, Module#alias_method can often be used instead of +alias+. + +You can also use +alias+ to alias global variables: + + $old = 0 + + alias $new $old + + p $new # prints 0 + +You may use +alias+ in any scope. + +== +undef+ + +The +undef+ keyword prevents the current class from responding to calls to the +named methods. + + undef my_method + +You may use symbols instead of method names: + + undef :my_method + +You may undef multiple methods: + + undef method1, method2 + +You may use +undef+ in any scope. See also Module#undef_method + +== +BEGIN+ and +END+ + ++BEGIN+ defines a block that is run before any other code in the current file. +It is typically used in one-liners with ruby -e. Similarly +END+ +defines a block that is run after any other code. + ++BEGIN+ must appear at top-level and +END+ will issue a warning when you use it +inside a method. + +Here is an example: + + BEGIN { + count = 0 + } + +You must use { and } you may not use +do+ and +end+. + +Here is an example one-liner that adds numbers from standard input or any files +in the argument list: + + ruby -ne 'BEGIN { count = 0 }; END { puts count }; count += gets.to_i' + diff --git a/doc/syntax/modules_and_classes.rdoc b/doc/syntax/modules_and_classes.rdoc index 2c8f5321e0..f4ab1ea6f9 100644 --- a/doc/syntax/modules_and_classes.rdoc +++ b/doc/syntax/modules_and_classes.rdoc @@ -93,8 +93,9 @@ nesting: end end -However, if you use :: to define A::B without nesting -it inside +A+ a NameError exception will be raised because the nesting does not include +A+: +However, if you use :: to define A::B without +nesting it inside +A+ a NameError exception will be raised because the nesting +does not include +A+: module A Z = 1 @@ -193,6 +194,12 @@ The third visibility is +private+. A private method may not be called with a receiver, not even +self+. If a private method is called with a receiver a NoMethodError will be raised. +=== +alias+ and +undef+ + +You may also alias or undefine methods, but these operations are not +restricted to modules or classes. See the {miscellaneous syntax +section}[rdoc-ref:syntax/miscellaneous.rdoc] for documentation. + = Classes Every class is also a module, but unlike modules a class may not be mixed-in to -- cgit v1.2.1