0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

[py2rb] partition, rpartition

Posted at

はじめに

移植やってます。
( from python 3.7 to ruby 2.7 )

partition, rpartition (Python)

s = 'a,b,c'

print(s.partition(','))
print(s.rpartition(','))
print(s.partition('/'))
print(s.rpartition('/'))
print(s.partition(''))
print(s.rpartition(''))

('a', ',', 'b,c')
('a,b', ',', 'c')
('a,b,c', '', '')
('', '', 'a,b,c')
ValueError: empty separator

文字列を区切りますが、空文字の場合エラーとなります。

partition, rpartition (Ruby)

s = 'a,b,c'

p s.partition(',')
p s.rpartition(',')
p s.partition('/')
p s.rpartition('/')
p s.partition('')
p s.rpartition('')

["a", ",", "b,c"]
["a,b", ",", "c"]
["a,b,c", "", ""]
["", "", "a,b,c"]
["", "", "a,b,c"]
["a,b,c", "", ""]

ruby でも同様のメソッドがありますが、空文字の場合の動作が異なります。

メモ

  • Python の partition, rpartition を学習した
  • 百里を行く者は九十里を半ばとす
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?