Man hat ja beim Programmieren nicht immer ein Ruby-Buch zur Hand.
Aber daran haben die Ruby-Entwickler gedacht und mit dem Programm
ri
eine eigene Hilfe eingebaut (natürlich nur, wenn
Sie diese auch mit installiert haben).
ri steht für
„Ruby Index“.
Jetzt haben wir eine typische Henne-Ei-Situation. Wie kann ich
Ihnen die Hilfe zu Ruby erklären, wenn wir gerade erst mit Ruby
anfangen? Ich greife also voraus und zeige Ihnen, wie Sie Informationen
zur Klasse
String
suchen können:
sw@debian:~/sandbox$ ri String
= String < Object
------------------------------------------------------------------------------
= Includes:
Comparable (from ruby site)
(from ruby site)
------------------------------------------------------------------------------
A String object holds and manipulates an arbitrary sequence of bytes,
typically representing characters. String objects may be created using
String::new or as literals.
Because of aliasing issues, users of strings should be aware of the methods
that modify the contents of a String object. Typically, methods with names
ending in ``!'' modify their receiver, while those without a ``!'' return a
new String. However, there are exceptions, such as String#[]=.
Rake extension methods for String.
[...]
Suchen wir jetzt Informationen zu einer bestimmten Methode
(Henne-Ei!), dann geht das auch mit
ri
. Als Beispiel
nehmen wir mal
gsub
. Das ist eine Methode, mit
der sich Teile in einem
String
austauschen lassen
(immer wieder praktisch).
sw@debian:~/sandbox$ ri String.gsub
= String.gsub
(from ruby site)
------------------------------------------------------------------------------
str.gsub(pattern, replacement) -> new_str
str.gsub(pattern, hash) -> new_str
str.gsub(pattern) {|match| block } -> new_str
str.gsub(pattern) -> enumerator
------------------------------------------------------------------------------
Returns a copy of str with the all occurrences of
pattern substituted for the second argument. The
pattern is typically a Regexp; if given as a String, any regular
expression metacharacters it contains will be interpreted literally, e.g.
'\\\d' will match a backlash followed by 'd', instead of a digit.
If replacement is a String it will be substituted for
the matched text. It may contain back-references to the pattern's capture
groups of the form \\\d, where d is a group number, or \\\k<n>, where n is
a group name. If it is a double-quoted string, both back-references must be
preceded by an additional backslash. However, within
replacement the special match variables, such as &$,
will not refer to the current match.
If the second argument is a Hash, and the matched text is one of its keys, the
corresponding value is the replacement string.
In the block form, the current match string is passed in as a parameter, and
variables such as $1, $2, $`, $&, and $' will be set appropriately. The value
returned by the block will be substituted for the match on each call.
The result inherits any tainting in the original string or any supplied
replacement string.
When neither a block nor a second argument is supplied, an Enumerator is
returned.
"hello".gsub(/[aeiou]/, '*') #=> "h*ll*"
"hello".gsub(/([aeiou])/, '<\1>') #=> "h<e>ll<o>"
"hello".gsub(/./) {|s| s.ord.to_s + ' '} #=> "104 101 108 108 111 "
"hello".gsub(/(?<foo>[aeiou])/, '{\k<foo>}') #=> "h{e}ll{o}"
'hello'.gsub(/[eo]/, 'e' => 3, 'o' => '*') #=> "h3ll*"