LoginSignup
0
0

More than 3 years have passed since last update.

"~"で任意の文字を囲む方法

Posted at

【概要】

1.結論

2.どのようにコーディングするか

3.開発環境

1.結論

gets.chomp、lengthメソッドを使用する!!


2.どのようにコーディングするか

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

このようにすると、下記のようになります。
スクリーンショット 2020-10-17 15.41.09.png

❶:.chompを入れないと下記のようになります。改行をして2行目の最後の"~"をくっつけています。
スクリーンショット 2020-10-17 15.42.50.png
❷:lengthメソッドで入力したの文字の長さに +2しています。+2する理由は左上の"~"と右上の"~"の角っこを加えています。
❸:入力した文字の両端に"~”を加えています。

3.開発環境

Mac catalina 10.15.4
Vscode
Ruby 2.6.5
Rails 6.0.3.3

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