Method memorandum and first post that I googled when checking a certain skill
qiita.rb
hoge #Characters to be entered
huga #Characters to be entered
piga #Characters to be entered
lines = [] #Define array variables in advance
lines << gets.chomp #Chomp so there are no line breaks
-Since the character string comes over multiple lines, add it to the array
qiita.rb
lines = ["hoge", "fuga", "piga"]
puts lines[0] #Output any character by specifying the index number (subscript)
>> hoge
-Output any character with the index number after making necessary changes according to the calling conditions.
qiita.rb
#Conditional branch
String.end_with?('Value you want to find') #任意の文字末尾にValue you want to findがあるか
puts hoge.end_with?('e', 'ge', 'ch') #Multiple values can be referenced at once (any one is acceptable)
>> ture
#Insert method
String.insert(Insertion index number,"Value to insert")
#['h','o','g','e']The index number of is from the beginning[0,1,2,3], From the end[-4,-3,-2,-1]
puts hoge.insert(-1,"s") #Insert at the end
>> hoges
puts hoge.insert(0,"s") #Insert at the beginning
>> shoge
#Destructive method
String.delete_suffix("Value to erase") #末尾のValue to eraseを消去
puts gehoge.delete_suffix("ge")
>> geho #The beginning does not disappear
#Replace (end)"ge"To"ver"Convert to)
puts gehoge.delete_suffix("ge").insert(-1,"ver")
>> hehover
reference https://qiita.com/uuchan/items/a4e9382440bdc4d2ac75 https://qiita.com/prgseek/items/92b49fe6b0a579f9cdd8
I wish I could replace it quickly, but it was difficult, so I pushed it with a matching technique If you know a better way, please comment!
Recommended Posts