情報学部 | 菅沼ホーム | 目次 | 索引 |
p "abc" + "efg" # => "abcefg"
p "abc" * 3 # => "abcabcabc"
p "%.3f" % 3.141592654 # => "3.142" p "%.3f %.3f" % [3.141592654, 1.234567] # => "3.142 1.235"
p "abc" << "efg" # => "abcefg" p "abc" << 65 # => "abcA"
p "xyabc" =~ /a./ # => 2 p /a./ =~ "xyabc" # => 2
p "abcdea"[0] # => 97 # a のコード p "abcdea"[1] # => 98 p "abcdea"[-1] # => 97 str = "abcdea"; str.slice!(1); p str # => "acdea"
p "abcdea"[1, 3] # => "bcd" p "abcdea"[-3, 2] # => "de" str = "abcdea"; str.slice!(1,3); p str # => "aea"
p "abcdea"["cde"] # => "cde" str = "abcdea"; str.slice!("cde"); p str # => "aba"
p "xyabcdasea"[/a./] # => "ab" p "xyabcdasea"[/(a.)\w+(s.)/, 2] # => "se" p "xyabcdasea"[/(a.)\w+(s.)/, 0] # => "abcdase" str = "xyabcdasea"; str.slice!(/a./); p str # => "xycdasea"
p "abcdefg"[2..4] # => "cde" p "abcdefg"[2...4] # => "cd" p "abcdefg"[4..2] # => "" p "abcdefg"[4..-2] # => "ef" p "abcdefg"[-2..-4] # => "" p "abcdefg"[-4..-2] # => "def" p "abcdefg"[5..9] # => "fg" p "abcdefg"[9..10] # => nil str = "abcdefg"; str.slice!(2 .. 4); p str # => "abfg"
a = "abc"; a[1] = "xxx"; p a # => "axxxc" a = "abc"; a[1] = 97; p a # => "aac" a = "abc"; a[-1] = "xxx"; p a # => "abxxx"
a = "abcdefg"; a[2, 3] = "xxxxx"; p a # => "abxxxxxfg" a = "abcdefg"; a[-5, 3] = "xxxxx"; p a # => "abxxxxxfg"
a = "abcdefg"; a["cde"] = "xxxxx"; p a # => "abxxxxxfg"
a = "xyabcdasea"; a[/a./] = "xxx"; p a # => "xyxxxcdasea" a = "xyabcdasea"; a[/(a.)\w+(s.)/, 2] = "xxx"; p a # => "xyabcdaxxxa" a = "xyabcdasea"; a[/(a.)\w+(s.)/, 0] = "xxx"; p a # => "xyxxxa"
a = "abcdefg"; a[2..4] = "xxxxx"; p a # => "abxxxxxfg"
p "abc" <=> "efg" # => -1
p "SUGANUMA".capitalize # => "Suganuma"
p "Abc" <=> "abc" # => -1 p "Abc".casecmp("abc") # => 0
p "abcd".center(10, "+") # => "+++abcd+++"
p "abc\n".chomp() # => "abc"
p "abc".chop # => "abc" p "abc".chop # => "ab"
p "abcdefghijk".count("ab", "abc") # => 2 p "abcdefghijk".count("a-h", "^cde") # => 5 p "abcdefghijk".count("a-h", "^c-e") # => 5
p "abcde".crypt("ango") # => "anxVSVhtG9Adk"
p "abcdefghijk".delete("ab", "abc") # => "cdefghijk" p "abcdefghijk".delete("a-h", "^cde") # => "cdeijk" p "abcdefghijk".delete("a-h", "^c-e") # => "cdeijk"
p "ABcd".downcase # => "abcd"
str = "" "a\nbc\ndef\n".each("\n") { |line| str += line.chomp } p str # "abcdef" を出力
str = "" "ABCD".each_byte { |byte| c = byte + 32; str <<= c } p str # "abcd" を出力
p "ABCDEFGABCDE".gsub("CDE", "cde") # => "ABcdeFGABcde" p "ABCDEFGABCDE".gsub(/C../, "cde") # => "ABcdeFGABcde" p "ABCDEFGABCDE".gsub(/C../, "x\\&x") # => "ABxCDExFGABxCDEx" p "ABCDEFGABCDE".gsub(/C../) { |m| m.downcase } # => "ABcdeFGABcde"
p "ff".hex # => 255
p "abcdefcdef".index("cde") # => 2 p "abcdefcdef".index(/c../, 3) # => 6 p "abcdefcdef".index(/c../, -8) # => 2
p "abcd".insert(2, "XXX") # => "abXXXcd"
p "abcd".intern # => :abcd p "abcd".intern.to_s # => "abcd"
p "abcdefyycxxk".match(/(c..)\w+(c..)/)[0] # => "cdefyycxx" p "abcdefyycxxk".match(/(c..)\w+(c..)/)[1] # => "cde" p "abcdefyycxxk".match(/(c..)\w+(c..)/)[2] # => "cxx"
p "abc".next # => "abd" p "abz".next # => "aca" p "100".next # => "101"
p "10".oct # => 8 p "0b10".oct # => 2 p "0x10".oct # => 16
str = "abc"; str.replace("efg"); p str # => "efg"
p "abcdef".reverse # => "fedcba"
p "abcdefcdef".rindex("cde") # => 6 p "abcdefcdef".rindex(/c../, 5) # => 2 p "abcdefcdef".rindex(/c../, -5) # => 2
p "abcaxy".scan(/a./) p "abcaxy".scan(/(a.)/) "abcaxy".scan(/a./) { |x| p x } "abcaxy".scan(/(a.)/) { |x| p x }
["ab", "ax"] [["ab"], ["ax"]] "ab" "ax" ["ab"] ["ax"]
p "xx xayyybzzz".split(/[a,b]/) # => ["xx x", "yyy", "zzz"] p "xx xayyybzzz".split(/([a,b])/) # => ["xx x", "a", "yyy", "b", "zzz"] p "xx xayyybzzz".split("a") # => ["xx x", "yyybzzz"] p "xx xayyybzzz".split("yyy") # => ["xx xa", "bzzz"] p "xx xayyybzzz".split(nil) # => ["xx", "xayyybzzz"] p "xx xayyybzzz".split("") # => ["x", "x", " ", "x", "a", "y", "y", "y", "b", "z", "z", "z"] p "xx xayyybzzz".split("", 5) # => ["x", "x", " ", "x", "ayyybzzz"]
p "aaabbbcccdddeee".squeeze # => "abcde" p "aaabbbcccdddeee".squeeze("a-z", "^bc") # => "abbbcccde"
p "ABCDEFGABCDE".sub("CDE", "cde") # => "ABcdeFGABCDE" p "ABCDEFGABCDE".sub(/C../, "cde") # => "ABcdeFGABCDE" p "ABCDEFGABCDE".sub(/C../, "x\\&x") # => "ABxCDExFGABCDE" p "ABCDEFGABCDE".sub(/C../) { |m| m.downcase } # => "ABcdeFGABCDE"
p "abcEFG".swapcase # => "ABCefg"
p "1.23e-2".to_f # => 0.0123
p "10".to_i # => 10 p "10".to_i(16) # => 16 p "0x10".to_i(0) # => 16
p "accCdffF".tr("cf", "CF") # => "aCCCdFFF" p "accCdffF".tr_s("cf", "CF") # => "aCCdFF" p "accCdffF".tr("a-z", "A-Z") # => "ACCCDFFF" p "accCdffF".tr_s("a-z", "A-Z") # => "ACCDFF"
p "accCdffF".tr("cf", "CF") # => "aCCCdFFF" p "accCdffF".tr_s("cf", "CF") # => "aCCdFF" p "accCdffF".tr("a-z", "A-Z") # => "ACCCDFFF" p "accCdffF".tr_s("a-z", "A-Z") # => "ACCDFF"
p "ABcd".upcase # => "ABCD"
str = ""; "abc".upto("abe") { |s| str += s }; p str # => "abcabdabe"
情報学部 | 菅沼ホーム | 目次 | 索引 |