Help us understand the problem. What is going on with this article?

Ruby 文字列を任意の文字数に分割する

More than 5 years have passed since last update.

1文字ずつ分割するなら簡単だけど、2文字以上で分割するにはどうするんだっけ?と思い調べてみた。

1文字ずつ分割する場合

s = "1234567890"
s.split(//)
=> ["1", "2", "3", "4", "5", "6", "7", "8", "9", "0"]

n文字ずつに分割する場合

s = "1234567890"
n = 3
s.scan(/.{1,#{n}}/)
=> ["123", "456", "789", "0"]
paty-fakename
エンジニアっぽい仕事をしていて、なんとなく記事を投稿しようと思ったんです。
http://heart-shaped-chocolate.hatenablog.jp/
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした