LoginSignup
28
20

More than 5 years have passed since last update.

アルファベットの配列つくる

Posted at

* (Splat Operator) を使うといろんな書き方できますね。

アルファベットの配列を作ってみます。

('a'..'z').to_a
# => ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]

こう書けますね。

[*'a'..'z']
# => ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]

ちな

不要なアルファベットがあるときはこんなかんじ?

[*'a'..'z'].delete_if {|a| a =~ /[p|x|z|]/}
# => ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "q", "r", "s", "t", "u", "v", "w", "y"]

もっといろいろできる

REF

28
20
1

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
28
20