2
2

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 5 years have passed since last update.

動的にクラス名やテーブル名を取得・生成する際に便利なメソッド

Last updated at Posted at 2015-10-19

String#pluralize

単数形から複数形に変換

irb(main):014:0> ['phone', 'wolf', 'fish', 'class'].map(&:pluralize)
=> ["phones", "wolves", "fish", "classes"]

String#singularize

複数形から単数形に変換

irb(main):015:0> ["phones", "wolves", "fish", "classes"].map(&:singularize)
=> ["phone", "wolf", "fish", "class"]

String#underscore

クラス名からファイル名に変換

irb(main):016:0> "ActiveRecord::Base".underscore
=> "active_record/base"

String#camelize

ファイル名からクラス名に変換

irb(main):017:0> "active_record/base".camelize
=> "ActiveRecord::Base"

String#classify

テーブル名からクラス名に変換

irb(main):006:0> ['people', 'wolves'].map(&:classify)
=> ["Person", "Wolf"]

String#tableize

クラス名からテーブル名に変換

irb(main):007:0> ["Person", "Wolf"].map(&:tableize)
=> ["people", "wolves"]
2
2
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
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?