Tbpgr Blog

Employee Experience Engineer tbpgr(てぃーびー) のブログ

ActiveSupport

ActiveSupport | String#parameterize

概要 String#parameterize 詳細 String#parameterize について String#parameterize 指定文字列に含まれている[a-z0-9\-_]以外の文字を引数sepに置き換えます。 sepを指定しなかった場合は半角ハイフンになります。 サンプル # encoding: utf-8 require 'act…

ActiveSupport | String#demodulize

概要 String#demodulize 詳細 String#demodulize について String#demodulize モジュール::モジュール::任意の名称,などの文字列から右端の任意の名称を取得する。 サンプル # encoding: utf-8 require 'active_support/core_ext/string/inflections' requir…

ActiveSupport | String#dasherize

概要 String#dasherize 詳細 String#dasherize について String#dasherize 文字列をハイフン区切りにした結果を受け取る サンプル # encoding: utf-8 require 'active_support/core_ext/string/inflections' require 'tbpgr_utils' bulk_puts_eval binding, <…

ActiveSupport | String#titleize

概要 String#titleize 詳細 String#titleize について String#titleize 文字列を区切り文字ごとに先頭大文字にする。 サンプル # encoding: utf-8 require 'active_support/core_ext/string/inflections' require 'tbpgr_utils' bulk_puts_eval binding, <<-…

ActiveSupport | String#underscore

概要 String#underscore 詳細 String#underscore について String#underscore 文字列をスネークケースにした結果を受け取る サンプル # encoding: utf-8 require 'active_support/core_ext/string/inflections' require 'tbpgr_utils' bulk_puts_eval bindin…

ActiveSupport | String#camelize

概要 String#camelize 詳細 String#camelize について String#camelize 文字列をキャメルケースにした結果を受け取る 引数に「:lower」を与えると、最初の文字が小文字になる サンプル # encoding: utf-8 require 'active_support/core_ext/string/inflectio…

ActiveSupport | String#singularize

概要 String#singularize 詳細 String#singularize について String#singularize 文字列の単数形を返却する。 サンプル # encoding: utf-8 require 'active_support/core_ext/string/inflections' require 'tbpgr_utils' bulk_puts_eval binding, <<-EOS 'he…

ActiveSupport | String#pluralize

概要 String#pluralize 詳細 String#pluralize について String#pluralize 文字列の複数形を返却する。 plural は「複数」の意。引数に数値を与えた場合、1の場合は単数形。それ以外は複数形を返却します。 何かの結果サイズによって文言を変更したい時など…

ActiveSupport | String#at,from,to,first

概要 String#at,from,to,first 詳細 String#at,from,to,first について String#at,from,to,first 文字列の位置指定抽出関連のメソッド群。at任意のインデックスの文字を取得 from任意のインデックス以降の文字列を取得 to任意のインデックスまでの文字列を取…

ActiveSupport | String#indent

概要 String#indent 詳細 String#indent について String#indent テキストをインデントしてくれる。内部のインデント形式に合わせてスペースインデントやタブインデントを行ってくれる。 インデント形式を明示的に指定することも可能。 サンプル # encoding:…

ActiveSupport | String#strip_heredoc

概要 String#strip_heredoc 詳細 String#strip_heredoc について String#strip_heredoc ヒアドキュメントをインデントの開始位置をインデントの1階層目として 不要な空白を除去してくれる。ヒアドキュメントはこういう風に書けたらいいのに、と思っていたの…

ActiveSupport | StringInquirer

概要 StringInquirer 詳細 StringInquirer について StringInquirer 文字列比較を構文の一部のように見せる機能を提供する。例えば Rails.env = "production" サンプル # encoding: utf-8 require 'active_support/core_ext' env = "production" p env == "p…

ActiveSupport | String#truncate

概要 String#truncate 詳細 String#truncate について String#truncate 指定文字数以降を除去した文字列を返す。 デフォルトでは最後の3文字を...にして返却する。よくブログ等で見る、「...続き」などの用途に使える。 デフォルト以外の文字を設定したけれ…

ActiveSupport | String#squish

概要 String#squish 詳細 String#squish について String#squish squish = 圧搾。 文字列中の改行文字を取り除き、連続したスペースを一つにする。 サンプル # encoding: utf-8 require 'active_support/core_ext/string/filters' p " test1 test2\ntest3\rt…

ActiveSupport | Class#descendants

概要 Class#descendants 詳細 Class#descendants について Class#descendants 子孫クラスを取得する。descendants=子孫 サブクラス、サブクラスのサブクラス等特定クラスから継承された全てのクラスを取得する。 サンプル # encoding: utf-8 require 'activ…

ActiveSupport | Class#subclasses

概要 Class#subclasses 詳細 Class#subclasses について Class#subclasses サブクラスを取得する。 サンプル # encoding: utf-8 require 'active_support/core_ext/class/subclasses' require 'tbpgr_utils' class A;end class B < A;end class D < A;end cl…

ActiveSupport | Module#class_attribute

概要 Module#class_attribute 詳細 Module#class_attribute について Module#class_attribute クラスレベルの属性を設定する。 継承先でも引き継がれる。 インスタンスからも利用可能。 instance_readerオプション、instance_writerオプションの指定によって…

ActiveSupport | Module#delegate

概要 Module#delegate 詳細 Module#delegate について Module#delegate メソッドの委譲を行う。 複数のメソッドの一括委譲も可能。toオプションで委譲オブジェクトの指定 prefixオプションでプリフィックスの有無を設定(省略するとプリフィックスなし) all…

ActiveSupport | Module#parent

概要 Module#parent 詳細 Module#parentについて Module#parent 親モジュールを取得する。 トップレベルまで到達した場合はObjectを返却する。 サンプル # encoding: utf-8 require 'active_support/core_ext/module/introspection' require 'tbpgr_utils' m…

ActiveSupport | Module#alias_attribute

概要 Module#alias_attribute 詳細 Module#alias_attributeについて Module#alias_attribute Attributeにエイリアスを付与する。 サンプル # encoding: utf-8 require 'active_support/core_ext/module/aliasing' require 'tbpgr_utils' require 'attributes…

ActiveSupport | Module#alias_method_chain (公式サイトに載っていないブロック呼び出しのサンプル付)

概要 Module#alias_method_chain 詳細 Module#alias_method_chainについて Module#alias_method_chain メソッドにエイリアスを付与する際に、old_with_new,old_without_new という名称のエイリアスを生成する。(メソッド名の衝突防止) ※old=古いメソッド名…

ActiveSupport | Object#in?

概要 Object#in? 詳細 Object#in?について Object#in? 対象オブジェクトのinclude?を呼び出し、結果を真偽値で返却します。 配列、文字列、範囲など種類を問わず指定した値が含まれているか判定したい場合に有用です。 サンプル # encoding: utf-8 require "…

ActiveSupport | reporting

概要 reporting 詳細 reportingについて 一覧 method 内容 Kernel#enable_warnings $VERBOSEをtrueにしてブロックを実行する。ブロック実行後、$VERBOSEは処理前の値に戻す Kernel#slice_warnings $VERBOSEをnilにしてブロックを実行する。ブロック実行後、$…

ActiveSupport | Object#instance_values/instance_variable_names

概要 Object#instance_values/instance_variable_names 詳細 Object#instance_values/instance_variable_namesについて Object#instance_values/instance_variable_names instance_values:インスタンス変数の値をハッシュで取得する。 instance_variable_na…

ActiveSupport | Object#with_options

概要 Object#with_options 詳細 Object#with_optionsについて Object#with_options オプションを受け取るメソッドに対して、一部のオプションは固定・一部のオプションは可変、 のような場合に固定部を繰り返し記述しない済む。 詳しくはサンプルを参照 サン…

ActiveSupport | Object#to_query

概要 Object#to_query 詳細 Object#to_paramについて Object#to_query クエリ文字列を作成する際に利用される。 Arrayはキーが空で "user=aaa&user=bbb"のようになる。 Hashはクエリー文字列を想定しているため、aaa='aaa'&bbb='bbb'&ccc='ccc'のような文字…

ActiveSupport | Object#to_param

概要 Object#to_param 詳細 Object#to_paramについて Object#to_param URLおよびクエリ文字列を作成する際に利用される。 基本はselfを返却する。 ArrayはURLを想定しているため、/区切りの文字列を返却する。 Hashはクエリー文字列を想定しているため、aaa=…

ActiveSupport | Object#acts_like?

概要 Object#acts_like? 詳細 Object#acts_like?について Object#acts_like? acts_like_xxx?を実装している場合に acts_like? :xxxを呼び出すと真を返却する。ダックタイピングを利用する際のマーカーとして利用する。 サンプル # encoding: utf-8 require "…

ActiveSupport | Kernel#class_eval

概要 Kernel#class_eval 詳細 Kernel#class_evalについて Kernel#class_eval 特異クラス(singleton_class)のclass_evalを呼び出すのと同等。 サンプル # encoding: utf-8 require "active_support/core_ext/kernel/singleton_class" require "tbpgr_utils"…

ActiveSupport | Object#try

概要 Object#try 詳細 Object#tryについて Object#try nilでは無い時だけメソッドを実行したい場合に利用する。 nilの場合は例外は発生せず、nilを返却するObject#tryはメソッドが存在しない場合にエラーにならない。(nilを返却する) Object#try!はメソッ…