LoginSignup
1
1

More than 5 years have passed since last update.

整数を指定桁数にするだけのgemを作ってみました

Last updated at Posted at 2014-02-18
#{year}-#{month}-#{day}

が「2014-02-18」になって欲しいのに「2014-2-18」なってしまう件への対応だとか

hoge_23.zipというファイル名がhoge_9.zipよりも先に並んでしまう問題を解決するために
「hoge_23.zip」ではなくて「hoge_0023.zip」というように頭に0を付けて桁数を合わせるようにしたい
というケースが度々あったのでgemにしてみました。

$ bundle gem zerostr

してから編集したのは下記の17行。
https://github.com/pandeiro245/zerostr/commit/03365eeaebbd6ede18a7845b0629ccecb605f4c9

「to_zerostr」とか名前の付け方超適当なのでオススメがあれば教えてください…。

もちろんテストとか例外処理とかやっていかないといけないけど
めっちゃ簡単にできたのでこれからも
ちょこちょこgem化していきたいと思いました。

 gem 'zerostr', :path => '../zerostr'

公開しなくても上記のようにRailsのGemfileに書けば
下記のように動きます。

> 2.to_zerostr
=> "02"
> 18.to_zerostr
=> "18"
> 23.to_zerostr(4)
=> "0023"

参考にさせて頂いた記事:
http://soplana.hateblo.jp/entry/2012/06/01/234259

1
1
2

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
1