LoginSignup
0
0

More than 3 years have passed since last update.

Ruby getsについて

Posted at

getsの使い方

getsを使用することで、入力された値を所得できる

input = gets

gets.chompの使い方

getsを使用して所得した値には、文末に改行が入る

getsの後に.chompとつけることで、改行をなくすことができる

#.chompを使用しない場合
input = gets
puts "a" + input + "b"

getsでbを入力した場合の出力結果は

ab
c

となる

#.chompを使用する場合
input = gets.chomp
puts "a" + input + "b"

出力結果は

abc

となる

gets.to_iの使い方

getsの後に.to_iとつけることで、所得した値を数値に変換できる
iはinteger(整数)の頭文字

input = gets.to_i 

gets.to_sの使い方

getsの後に.to_sとつけることで、所得した値を文字列に変換できる
sはstring(コンピューター用語で文字列)の頭文字

input = gets.to_s
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