|
Dokumentation
|
| |
Semantik von Strings |
|
Ruby 1.8 |
Strings bestehen aus einer Folge von Zeichen,
diese Zeichen können die Werte von 0 bis 255 annehmen.
|
|
Also keine vernünftige Unicode Unterstützung
|
|
Ruby 1.9 hat Unicode Unterstützung
|
Alle Methoden |
"a".methods.sort
=> ["%", "*"
, "+"
, "<", "<<", "<=", "<=>"
, "==", "===", "=~", ">", ">="
, "[]", "[]="
, "__id__", "__send__"
, "all?", "any?", "between?"
, "capitalize", "capitalize!"
, "casecmp", "center"
, "chomp", "chomp!", "chop", "chop!"
, "class", "clone", "collect"
, "concat", "count", "crypt"
, "delete", "delete!", "detect", "display"
, "downcase", "downcase!", "dump", "dup"
, "each", "each_byte"
, "each_line", "each_with_index"
, "empty?"
, "entries", "eql?"
, "equal?", "extend"
, "find", "find_all"
, "freeze", "frozen?"
, "grep", "gsub", "gsub!"
, "hash", "hex", "id", "include?"
, "index", "inject", "insert"
, "inspect", "instance_eval"
, "instance_of?", "instance_variable_defined?"
, "instance_variable_get"
, "instance_variable_set"
, "instance_variables"
, "intern", "is_a?", "kind_of?"
, "length", "ljust", "lstrip", "lstrip!"
, "map", "match"
, "max", "member?"
, "method", "methods"
, "min"
, "next", "next!", "nil?"
, "object_id", "oct"
, "partition"
, "private_methods"
, "protected_methods", "public_methods"
, "reject", "replace", "respond_to?"
, "reverse", "reverse!"
, "rindex", "rjust", "rstrip", "rstrip!"
, "scan", "select", "send"
, "singleton_methods"
, "size", "slice", "slice!"
, "sort", "sort_by"
, "split"
, "squeeze", "squeeze!"
, "strip", "strip!"
, "sub", "sub!"
, "succ", "succ!"
, "sum", "swapcase", "swapcase!"
, "taint", "tainted?"
, "to_a", "to_f", "to_i", "to_s"
, "to_str", "to_sym"
, "tr", "tr!"
, "tr_s", "tr_s!"
, "type"
, "unpack", "untaint"
, "upcase", "upcase!", "upto"
, "zip"]
|
|
Welche Operationen sind wichtig?
|
Konkatenation |
"abc" + "xyz"
"abc" * 3
"%d" % (6 * 7)
"abc" << "xyz"
"abc" << (6 * 7)
s = "abc"
s << "def"
s
s << "xyz"
s
|
Selektoren |
"abc"[0]
"abc"[1]
"abc"[-1]
"abc"[0..1]
"abc"[0..0]
"abc"[1..0]
"abc"[1..27]
"abc"[3..27]
"abc"[4..27]
"abc"[-1..-1]
"abc"[-2..-1]
"abc"[-3..-1]
"abc"[-4..-1]
|
Zuweisungen und Bereichszuweisung |
a="abcd" ; a[1] = 120 ; a
a="abcd" ; a[1] = ?x ; a
a="abcd" ; a[1] = "120" ; a
a="abcd" ; a[1..0] = "120" ; a
a="abcd" ; a[1..2] = "xy" ; a
a="abcd" ; a[1..2] = "" ; a
|
|
Mit Bereichszuweisungen kann das Einfügen, das Löschen und das Ersetzen von Teilzeichenreihen realisisert werden.
|
Vergleiche, Tests und einfache Abfragen |
"a" <=> "a"
"" <=> "a"
"a" <=> ""
"a" == "a"
"a" == "b"
"a" > "a"
"a" > ""
"a" >= "a"
"a" >= "aa"
"a" < "a"
"a" < "b"
"a" < "aa"
"a" < ""
"a" <= "a"
"b" <= "a"
"a" =~ /a+/
"xay" =~ /a+/
"xyz" =~ /a+/
"a" =~ /x*/
"".empty?
"a".empty?
"a".length
"".length
"a".size
"".size
|
| |
Einfache Manipulation von Texten |
a="abc" ; b="ABC" ; c="AbC"
a.capitalize
a.upcase
b.downcase
c.swapcase
a.center(5)
a.ljust(5)
a.rjust(5)
a.chomp("c")
a.chomp("bc")
a.chomp("d")
a.chop
a.chop.chop
a.chop.chop.chop
a.chop.sui
a=" abc \n\t"
a.strip
a.reverse
a.reverse.reverse
"otto".reverse
|
| |
Suchen |
a="abcda"
a.include?("b")
a.include?("bc")
a.include?("x")
a.index("b")
a.index("cd")
a.index("x")
a.rindex("a")
a.rindex("bc")
a.rindex("x")
|
| |
Konversionen |
a="123"
a.to_i
a.to_f
a.to_s
|
| |
Operationen mit regulären Ausdrücken |
a="aabbccaa"
a.gsub(/a/,"A")
a.gsub(/bb/,"")
a.sub(/a/,"A")
a.sub(/bb/,"")
a=" 1 3 5 "
a.gsub(/ +/,"")
a="123.456.789"
r=/^([0-9]+)[.]([0-9]+)[.]([0-9]+)$/
s="\\3.\\2.\\1"
a.gsub(r,s)
a="xyz"
a.gsub(r,s)
a="123.456.789"
a.scan(/[0-9]+/)
a.split(".")
a.split(/./)
a.split(/[.]/)
a.split(//)
a=" abc xyz "
a.split
|
| |
|