[RUBY] How to enclose any character with "~"

【Overview】

1. Conclusion </ b>

2. How to code </ b>

3. Development environment </ b>

  1. Conclusion

Use gets.chomp, length </ b> methods !!


2. How to code

str = gets.chomp #---❶
str_length = str.length
puts('~' * (str_length + 2)) #---❷
puts('~'+str+'~') #---❸
puts('~' * (str_length + 2)) #---❷

If you do this, it will look like this: スクリーンショット 2020-10-17 15.41.09.png

❶: If you do not enter .chomp, it will be as follows. A line break is made and the last "~" on the second line is attached. スクリーンショット 2020-10-17 15.42.50.png ❷: The length of the character entered by the length method is +2. The reason for +2 is to add the "~" in the upper left and the "~" in the upper right. ❸: "~" Is added to both ends of the entered character.

  1. Development environment

Mac catalina 10.15.4 Vscode Ruby 2.6.5 Rails 6.0.3.3

Recommended Posts