Ruby文字列操作(Python 3と比較)
本当はRuby学習ノートに書いておきたかったのですが、文字列操作という内容が確かに多すぎて、単独で抽出しました.
RubyとPythonの両方の言語では、単一引用符と二重引用符の文字列がサポートされています.あまり話さないで、直接料理を出します(比較がはっきりしているため、ルビーの中でできるだけかっこを外します)!
文字列に変数を埋め込む
文字列の組み込み方法
str * 3
str + other_str
文字列の比較
正規表現の一致
str.sub_str
str.capitalize(str.capitalize!)
str.casecmp(大文字と小文字を区別しない文字列比較)
str.center
str.chomp(str.chomp!)
str.chop(str.chop!)
str.concat(other_str)
str.count
str.crypt
str.delete(str.delete!)
str.downcase(str.downcase!)
str.dump
str.each_byte
str.each_line
str.empty?
str.equal?
str.eql?
str.hash
str.hex
str.index
str.insert
str.to_sym
str.size(str.length)
str.ljust(str.rjust)
str.lstrip(str.lstrip!)
str.rstrip(str.rstrip!)
str.strip(str.strip!)
str.sub(str.sub!)
str.succ,str.next(str.succ!, str.next!)
str.sum
str.swapcase(str.swapcase!)
str.to_f
str.to_i
str.to_s(str.to_str)
str.tr(str.tr!)
str.tr_s(str.tr_s!)
str.upcase(str.upcase!)
str.upto
%w[hello world]騒操作だよ!
to be continued
RubyとPythonの両方の言語では、単一引用符と二重引用符の文字列がサポートされています.あまり話さないで、直接料理を出します(比較がはっきりしているため、ルビーの中でできるだけかっこを外します)!
文字列に変数を埋め込む
# Ruby
[1] pry(main)> name1 = "Joe"
=> "Joe"
[2] pry(main)> name2 = "Mary"
=> "Mary"
[3] pry(main)> puts " #{name1}, #{name2} ?"
Joe, Mary ?
=> nil
# Python3
>>> name1 = "Joe"
>>> name2 = "Mary"
>>> print(f" {name1}, {name2} ?")
Joe, Mary ?
文字列の組み込み方法
# Ruby
[4] pry(main)> myStr = String.new("THIS IS TEST")
=> "THIS IS TEST"
[5] pry(main)> foo = myStr.downcase
=> "this is test"
[6] pry(main)>
[7] pry(main)> puts "#{foo}"
this is test
=> nil
[8] pry(main)>
# Python3
>>> myStr = str("THIS IS TEST")
>>> myStr
'THIS IS TEST'
>>> foo = myStr.lower()
>>> foo
'this is test'
str * 3
# Ruby
[9] pry(main)> aa = "hello world"
=> "hello world"
[10] pry(main)> aa * 3
=> "hello worldhello worldhello world"
# Python3
>>> aa = "hello world"
>>> aa * 3
'hello worldhello worldhello world'
str + other_str
# Ruby
[11] pry(main)> "hello" + " world"
=> "hello world"
[12] pry(main)> "hello" << " world"
=> "hello world"
# Python3
>>> "hello" + " world"
'hello world'
文字列の比較
# Ruby
[13] pry(main)> "hello" < "world"
=> true
[14] pry(main)> "hello" <=> "world"
=> -1
[15] pry(main)> "hello" == "world"
=> false
# Python3
>>> "hello" < "world"
True
>>> 0 if "hello" == "world" else (-1 if "hello" < "world" else 1)
-1
>>> "hello" == "world"
False
正規表現の一致
# Ruby
[18] pry(main)> "hello world" =~ /ll/
=> 2
[19] pry(main)> "hello world" =~ /o.*o/
=> 4
# Python3
>>> import re
>>> re.search("ll", "hello world")
<_sre.sre_match object="" span="(2," match="ll">
>>> re.search("o.*o", "hello world")
<_sre.sre_match object="" span="(4," match="o wo">
str.sub_str
# Ruby
[19] pry(main)> "hello world"[0,5]
=> "hello"
[20] pry(main)> "hello world"[0...5]
=> "hello"
# Python3
>>> "hello world"[0:5]
'hello'
>>> "hello world"[0:-6]
'hello'
str.capitalize(str.capitalize!)
# Ruby
[27] pry(main)> aa = "hello world"
=> "hello world"
[28] pry(main)> aa.capitalize
=> "Hello world"
[29] pry(main)> aa
=> "hello world"
[30] pry(main)> aa.capitalize!
=> "Hello world"
[31] pry(main)>
# Python3
>>> aa = "hello world"
>>> aa.capitalize()
'Hello world'
>>> aa
'hello world'
>>> aa = aa.capitalize()
>>> aa
'Hello world'
str.casecmp(大文字と小文字を区別しない文字列比較)
# Ruby
[34] pry(main)> "hello".casecmp "He5l5"
=> 1
[35] pry(main)> "hello".casecmp "HeLLo"
=> 0
[36] pry(main)> "hello".casecmp "world"
=> -1
# Python3
>>> "hello".lower() == "HeLLo".lower()
True
str.center
# Ruby
[41] pry(main)> "hello".center 10
=> " hello "
# Python3
>>> "hello".center(10)
' hello '
str.chomp(str.chomp!)
# Ruby
[46] pry(main)> "hello
".chomp!
=> "hello"
[47] pry(main)> "hello".chomp!
=> nil
[48] pry(main)> "hello
".strip!
=> "hello"
[49] pry(main)> "hello".strip!
=> nil
# Python3
>>> "hello
".strip()
'hello'
str.chop(str.chop!)
# Ruby
[50] pry(main)> "hello".chop
=> "hell"
[51] pry(main)> "hello".chop!
=> "hell"
# Python3
>>> "hello
"[:-1]
'hello'
str.concat(other_str)
# Ruby
[53] pry(main)> "hello".concat " world"
=> "hello world"
[3] pry(main)> aa = "hello"
=> "hello"
[4] pry(main)> aa.concat(" world")
=> "hello world"
[5] pry(main)> aa
=> "hello world"
# Python3
>>> aa = "hello"
>>> aa += " world"
>>> aa
'hello world'
str.count
# Ruby
[54] pry(main)> "hello".count 'l'
=> 2
# Python3
>>> "hello".count('l')
2
str.crypt
# Ruby
[55] pry(main)> "hello".crypt "world"
=> "woglQSsVNh3SM"
str.delete(str.delete!)
# Ruby
[57] pry(main)> "hello".delete "ho"
=> "ell"
# Python3
>>> "hello".replace("h", "").replace("o", "")
'ell'
str.downcase(str.downcase!)
# Ruby
[58] pry(main)> "Hello".downcase
=> "hello"
# Python3
>>> "Hello".lower()
'hello'
str.dump
# Ruby
[59] pry(main)> "Hello".dump
=> "\"Hello\""
str.each_byte
# Ruby
[64] pry(main)> "Hello world".each_byte{|substr| puts substr}
72
101
108
108
111
32
119
111
114
108
100
# Python3
>>> [print(ord(item)) for item in "Hello world"]
72
101
108
108
111
32
119
111
114
108
100
str.each_line
# Ruby
[67] pry(main)> "hello
world".each_line{|substr| puts substr}
hello
world
=> "hello
world"
# Python3
>>> [print(item) for item in "hello
world".split()]
hello
world
[None, None]
str.empty?
# Ruby
[69] pry(main)> "hello".empty?
=> false
[70] pry(main)> "".empty?
=> true
# Python3
>>> "hello" == ""
False
>>> "" == ""
True
str.equal?
# Ruby
[73] pry(main)> "hello".equal? "hello"
=> false
[74] pry(main)> aa = "hello"
=> "hello"
[75] pry(main)> aa.equal? aa
=> true
# Python3
>>> "hello" is "hello"
True
>>> "hello" == "hello"
True
>>> aa = ['hello', 'world']
>>> bb = ['hello', 'world']
>>> aa == bb
True
>>> aa is bb
False
str.eql?
# Ruby
[71] pry(main)> "hello".eql? "hello"
=> true
# Python3
>>> aa = ['hello', 'world']
>>> bb = ['hello', 'world']
>>> aa == bb
True
>>> aa is bb
False
str.hash
# Ruby
[76] pry(main)> "hello".hash
=> 4132711420684197129
# Python3
>>> hash("hello")
-7700562595699703259
str.hex
# Ruby
[77] pry(main)> "hello".hex
=> 0
[78] pry(main)> "afcde".hex
=> 720094
str.index
# Ruby
[84] pry(main)> "hello".index("e")
=> 1
[85] pry(main)> "hello".index("ll")
=> 2
[86] pry(main)> "hello world".index(/o.*o/)
=> 4
# Python3
>>> "hello".index("e")
1
>>> "hello".index("ll")
2
>>> "hello world".index("o.*o")
Traceback (most recent call last):
File "", line 1, in
ValueError: substring not found
>>>
str.insert
# Ruby
[87] pry(main)> "hello".insert(3, '---')
=> "hel---lo"
# Python3
# Python3 , ,
>>> aa = "hello"
>>> aa[:3] + '---' + aa[3:]
'hel---lo'
str.to_sym
# Ruby
[88] pry(main)> "hello".to_sym
=> :hello
str.size(str.length)
# Ruby
[89] pry(main)> "hello".size
=> 5
[90] pry(main)> "hello".length
=> 5
# Python3
>>> len("hello")
5
str.ljust(str.rjust)
# Ruby
[91] pry(main)> "hello".ljust(10, '-')
=> "hello-----"
[92] pry(main)> "hello".rjust(10, '-')
=> "-----hello"
# Python3
>>> "hello".ljust(10, '-')
'hello-----'
str.lstrip(str.lstrip!)
# Ruby
[93] pry(main)> " hello".lstrip
=> "hello"
[93] pry(main)> " hello".lstrip!
=> "hello"
# Python3
>>> " hello".lstrip()
'hello'
str.rstrip(str.rstrip!)
# Ruby
[94] pry(main)> "hello ".rstrip
=> "hello"
[95] pry(main)> "hello ".rstrip!
=> "hello"
# Python3
>>> "hello ".rstrip()
'hello'
str.strip(str.strip!)
# Ruby
[97] pry(main)> " hello ".strip
=> "hello"
[98] pry(main)> " hello ".strip!
=> "hello"
# Python3
>>> " hello ".strip()
'hello'
str.sub(str.sub!)
# Ruby
[99] pry(main)> "hello world".sub("world", "hello")
=> "hello hello"
[100] pry(main)> "hello world".sub!("world", "hello")
=> "hello hello"
[102] pry(main)> "hello world".sub("world"){|match| "hello"}
=> "hello hello"
[103] pry(main)> "hello world".sub("world"){"hello"}
=> "hello hello"
# Python3
>>> "hello world".replace("world", "hello")
'hello hello'
str.succ,str.next(str.succ!, str.next!)
# Ruby
[104] pry(main)> "hello".succ
=> "hellp"
[105] pry(main)> "hello".next
=> "hellp"
# Python3
# 26 , ,
# Python3
>>> aa = "hello"
>>> bb = aa[:-1] + chr(ord(aa[-1]) + 1)
>>> bb
'hellp'
>>>
str.sum
# Ruby
[106] pry(main)> "hello".sum
=> 532
[107] pry(main)> "hello".sum(n=16)
=> 532
str.swapcase(str.swapcase!)
# Ruby
[108] pry(main)> "HeLLo".swapcase
=> "hEllO"
[109] pry(main)> "HeLLo".swapcase!
=> "hEllO"
str.to_f
# Ruby
[110] pry(main)> "HeLLo".to_f
=> 0.0
[111] pry(main)> "12.34".to_f
=> 12.34
# Python3
>>> float("HeLLo")
Traceback (most recent call last):
File "", line 1, in
ValueError: could not convert string to float: 'HeLLo'
>>> float("12.34")
12.34
str.to_i
# Ruby
[118] pry(main)> "12.34".to_i
=> 12
[119] pry(main)> "a12.34".to_i
=> 0
# Python3
>>> int("12.34")
Traceback (most recent call last):
File "", line 1, in
ValueError: invalid literal for int() with base 10: '12.34'
>>> int("12")
12
str.to_s(str.to_str)
# Ruby
[120] pry(main)> "hello".to_s
=> "hello"
[121] pry(main)> "hello".to_str
=> "hello"
# Python3
>>> str("hello")
'hello'
str.tr(str.tr!)
# Ruby
[127] pry(main)> "hello".tr("he","g")
=> "ggllo"
[128] pry(main)> "hello".tr("he","gx")
=> "gxllo"
[129] pry(main)> "hello".tr("he","gxx")
=> "gxllo"
[130] pry(main)> "hello".tr("he","gxt")
=> "gxllo"
str.tr_s(str.tr_s!)
# Ruby
[134] pry(main)> "hello".tr_s("he","g")
=> "gllo"
[135] pry(main)> "hello".tr_s("he","gg")
=> "gllo"
[136] pry(main)> "hello".tr_s("he","gx")
=> "gxllo"
[137] pry(main)> "hello".tr_s("he","gxr")
=> "gxllo"
str.upcase(str.upcase!)
# Ruby
[138] pry(main)> "Hello".upcase
=> "HELLO"
[139] pry(main)> "Hello".upcase!
=> "HELLO"
# Python3
>>> "Hello".upper()
'HELLO'
str.upto
# Ruby
[131] pry(main)> "Hello".upto("Hellz"){|s| puts s}
Hello
Hellp
Hellq
Hellr
Hells
Hellt
Hellu
Hellv
Hellw
Hellx
Helly
Hellz
=> "Hello"
%w[hello world]騒操作だよ!
# Ruby
[3] pry(main)> %w[hello world]
=> ["hello", "world"]
to be continued
# Ruby
# Python3