目次
この記事はアピリッツの技術ブログ「DoRuby」から移行した記事です。情報が古い可能性がありますのでご注意ください。
こんにちは。
KBMJでRuby on Rails を使ったweb開発に携わっているyoppiといいます。
Ruby on Railsでは、Webフレームワーク機能以外にも、Ruby標準のクラスなどに関数の追加なども行われます。これらの関数は便利な割にそんなに知られていない気がするので、紹介してみようかな~と思います。
at
at(位置:integer)
文字列の指定した位置の文字を返します
例)
  “Hello”.at(0)  # => “H”
  “Hello”.at(4)  # => “o”
  “Hello”.at(10) # => nil
  “Hello”.at(-1)  # => “o” 最後から1文字
  “Hello”.at(-4)  # => “e”
  “Hello”.at(-10) # => nil
first
first(長さ:integer 初期値:1)
文字列の先頭から指定した長さの文字列を取得します
例)
  “Hello”.first  # => “H”
  “Hello”.first(3)  # => “Hel”
  “Hello”.first(10) # => “Hello”
  “Hello”.first(0)  # => “Hello”
  “Hello”.first(-3)  # => “He” 最後から4文字目から先頭
  “Hello”.first(-10) # => “”
from
from(位置:integer)
文字列の指定した位置から最後までの文字列を取得します
例)
  “Hello”.from(1)  # => “ello”
  “Hello”.from(3)  # => “lo”
  “Hello”.from(10) # => “”
  “Hello”.from(0)  # => “Hello”
  “Hello”.from(-2)  # => “lo” 最後から2文字目から最後
  “Hello”.from(-10) # => “”
last
last
文字列の最後から指定した長さの文字列を取得します
例)
  “Hello”.last(1)  # => “o”
  “Hello”.last(3)  # => “llo”
  “Hello”.last(10) # => “Hello”
  “Hello”.last(0)  # => “Hello”
  “Hello”.last(-2)  # => “llo” 先頭から2文字目から最後
  “Hello”.last(-10) # => “”
to
to
文字列の先頭から指定した位置までの文字列を取得します
例)
  “Hello”.to(1)  # => “He”
  “Hello”.to(3)  # => “Hell”
  “Hello”.to(10) # => “Hello”
  “Hello”.to(0)  # => “H”
  “Hello”.to(-2)  # => “Hell” 最後から2文字目まで
  “Hello”.to(-10) # => “”
starts_with?
starts_with?
文字列が指定した文字列で始まるかを判定します
例)
  “Hello”.starts_with?(“H”)  # => true
  “Hello”.starts_with?(“Hel”)  # => true
  “Hello”.starts_with?(“Helo”)  # => false
  “Hello”.starts_with?(“ello”)  # => false
ends_with?
ends_with?
文字列が指定した文字列で終わるかを判定します
例)
  “Hello”.ends_with?(“o”)  # => true
  “Hello”.ends_with?(“ello”)  # => true
  “Hello”.ends_with?(“Hel”)  # => false
  “Hello”.ends_with?(“oilo”)  # => false
camelize
camelize(先頭の文字をどうするか:シンボル(:lower小文字|:upper大文字 初期値:upper)
パス形式からRubyのクラス名にする。/は::に変換されます。
例)
  “active_record”.camelize #=> “ActiveRecord”
  “active_record”.camelize(:lower) #=> “activeRecord”
  “active_record/errors”.camelize #=> “ActiveRecord::Errors”
  “active_record/errors”.camelize(:lower) #=> “activeRecord::Errors”
classify
classify
テーブル名をRubyのクラス名にする
例)
  “egg_and_hams”.classify #=> “EggAndHam”
  “post”.classify #=> “Post”
constantize
constantize
文字列のオブジェクトが読み込まれていれば、そのオブジェクトを返す
例)
  “Module”.constantize #=> Module
  “Array”.constantize #=> Array
dasherise
dasherize
「_(アンダースコア)」を「-(ハイフン)」に置換する
例)
 “puni_puni” #=> “puni-puni”
demodulize
demodulize
モジュールの部分を取り除く
例)
  “ActiveRecord::CoreExtensions::String::Inflections”.demodulize #=> “Inflections”
  “Inflections”.demodulize #=> “Inflections”
foreign_key
foreign_key(単語間をアンダースコアで区切るか:boolean 初期値:true)
クラス名から外部キー名にする
例)
  “Message”.foreign_key #=> “message_id”
  “Message”.foreign_key(false) #=> “messageid”
  “Admin::Post”.foreign_key #=> “post_id”
humanize
humanize
アンダースコアで区切られた文字をスペース区切りにし、最初の単語の先頭の文字を大文字にする。また、「_id」があれば削除する。そのことで、人が読みやすい形にする
例)
  “employee_salary” #=> “Employee salary”
  “author_id” #=> “Author”
pluralize
pluralize
複数形にする
例)
  “post”.pluralize #=> “posts”
  “octopus”.pluralize #=> “octopi”
  “sheep”.pluralize #=> “sheep”
  “words”.pluralize #=> “words”
  “the blue mailman”.pluralize #=> “the blue mailmen”
  “CamelOctopus”.pluralize #=> “CamelOctopi”
singularize
singularize
単数形にする
例)
  “posts”.singularize #=> “post”
  “octopi”.singularize #=> “octopus”
  “sheep”.singluarize #=> “sheep”
  “word”.singluarize #=> “word”su
  “the blue mailmen”.singularize #=> “the blue mailman”
  “CamelOctopi”.singularize #=> “CamelOctopus”
tableize
tableize
クラス名をテーブル名にする
例)
  “RawScaledScorer”.tableize #=> “raw_scaled_scorers”
  “egg_and_ham”.tableize #=> “egg_and_hams”
  “fancyCategory”.tableize #=> “fancy_categories”
titleize
titleize
全ての単語の先頭の文字を大文字にする
例)
  “man from the boondocks”.titleize #=> “Man From The Boondocks”
  “x-men: the last stand”.titleize #=> “X Men: The Last Stand”
underscore
underscore
camelizeの逆の処理をする。名前空間を含めたクラス名をパスにする
例)
  “ActiveRecord”.underscore #=> “active_record”
  “ActiveRecord::Errors”.underscore #=> active_record/errors
まとめ
地味に役立ちそうなものが多い感じですかね。
camelizeとかはバンバン使えるようになりたいですよ。
 
            