1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

[Ruby]文字列を1文字ずつ配列として取得(備忘録)

Last updated at Posted at 2021-02-05

入力で文字列があった場合、それを1文字ずつ配列として取得したい場合の方法
例えば以下のような標準入力があった場合、


hello

次の記述で配列として取得できます

p gets.chomp.split("")
 # => ["h", "e", "l", "l", "o"]

split("")で空白のない文字を切り分けます。
chompは改行をなくすために採用しています。

ちなみに、以下の方法でもできるようです。

p gets.chomp.chars
 # => ["h", "e", "l", "l", "o"]

以上です。

1
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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?