LoginSignup
0
0

More than 3 years have passed since last update.

Ruby ユーザ の 入力文字列 を 変数 に 格納する

Posted at

目的

  • Rubyにてユーザからの入力文字列を受け取り、変数に格納する方法をまとめる。

書き方の例

  • 入力値を受け取るにはメソッドgetsを使用する。
  • 入力値の改行もなくして変数に格納したいためオプションchompを付ける。
  • 下記に処理を記載する。
変数 = gets.chomp

より具体的な例

  • 入力された文字列をコンソールに出力する処理を考える。
  • コンソールには「好きな文字列を入力してください。」 → 入力待ち → 「入力された文字列は○○です。」と表示されるようにする。
  • 入力された文字列は変数inputに格納する。
  • 下記に処理を記載する。
puts "好きな文字列を入力してください。"
input = gets.chomp
puts "入力された文字列は#{input}です。"
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