Tbpgr Blog

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

Ruby

Ruby | String | ascii_only?

概要 String#ascii_only? -> bool 詳細 文字列がASCII文字のみで構成されている場合に true を返却 サンプルコード require 'tbpgr_utils' bulk_puts_eval binding, <<-EOS 'hoge'.ascii_only? ''.ascii_only? 'ほgeascii_only?'.ascii_only? EOS __END__ 下…

Ruby | String | []=

概要 String#[nth] = val String#self[nth, len] = val String#self[substr] = val String#self[regexp, nth] = val String#self[regexp, name] = val String#self[regexp] = val String#self[range] = val 詳細 String#[nth] = val 文字列の nth 文字目を v…

Ruby | String | []

概要 String#self[nth] -> String | nil String#slice(nth) -> String | nil String#self[nth, len] -> String | nil String#slice(nth, len) -> String | nil 詳細 nth 番目の文字を返却。nth が正の場合: 文字列の先頭から数える nth が負の場合: 文字列の…

Ruby | String | =~

概要 String#=~ other -> Integer 詳細 正規表現 other とのマッチを実行。マッチが成功: マッチした位置のインデックス マッチが失敗: nilを返却。 other が正規表現でも文字列でもない場合は other =~ self を行います。このメソッドが実行されると、組み…

Ruby | String | ==

概要 String#self == other -> bool String#self === other -> bool 詳細 other が文字列の場合、文字列の内容を比較( String#eql? と同じ)。 other が文字列でない場合、 other.to_str が定義されていれば other == self の結果を返します。 (ただし、 othe…

Ruby | String |

概要 String# other -> -1 | 0 | 1 | nil 詳細 self と other を ASCII コード順で比較。 戻り値は以下のようになる。 self > other : 1 self == other : 0 self < other : -1このメソッドは Comparable モジュールのメソッドを実装するために利用する。 oth…

Ruby | String | <<

概要 String# self String#concat(other) -> self 詳細 self に文字列 other を破壊的に連結。 other が 0 から 255 のまでの整数の場合はASCIIコードで対応する文字を結合する。 self を返します。 サンプルコード require 'tbpgr_utils' base = "base" bul…

Ruby | Regexp | +

概要 Regexp#+ other -> String 詳細 自信の文字列と other を連結した新しい文字列を返却 サンプルコード require 'tbpgr_utils' base = 'hoge' bulk_puts_eval binding, <<-EOS base + 'add' base EOS __END__ 下記はTbpgrUtils gemの機能 bulk_puts_eval …

Ruby | String | *

概要 String#* times -> String 詳細 文字列の内容を times 回だけ繰り返した新しい文字列を返却。 サンプルコード require 'tbpgr_utils' bf =->(x) { '>%s.' % (?+ * x.ord) } bulk_puts_eval binding, <<-EOS 'hoge'*0 'hoge'*2 ?+*3 '+' '-'*3 '+%s-' % …

Ruby | String | %

概要 String#% 詳細 printf と同じ規則に従って args をフォーマットする。 サンプルコード require 'tbpgr_utils' bulk_puts_eval binding, <<-EOS 'hello %s world' % 'Ruby' 'hello %s world' % 'Java' '%-10s' % 'AWK' '%-10s' % 'C' '%#10s' % 'Perl' '…

Ruby | String | try_convert

概要 String.try_convert(obj) -> String | nil 詳細 obj を String に変換を試行。 変換には Object#to_str メソッドが使われる。変換成功時: 変換後文字列 変換失敗時: nil サンプルコード require 'tbpgr_utils' class Convertable def to_str "converted…

Ruby | String | new

概要 String#new(string = "") -> String 詳細 string と同じ内容の新しい文字列を作成して返却 サンプルコード require 'tbpgr_utils' bulk_puts_eval binding, <<-EOS "hoge" == String.new("hoge") "hoge".eql? String.new("hoge") "hoge".equal? String.…

Ruby | Regexp | to_s

概要 Regexp#to_s -> String 詳細 正規表現の文字列表現を生成して返却。 サンプルコード require 'tbpgr_utils' tmp = /(h.ge)(f.o)(b.r)/.to_s bulk_puts_eval binding, <<-EOS /h.ge/.to_s /(h.ge)(f.o)(b.r)/.to_s /#{tmp}/ =~ 'hogefoobarlast@@@' $~ /…

Ruby | Regexp | source

概要 Regexp#source -> String 詳細 その正規表現のもととなった文字列表現を生成して返却。 サンプルコード require 'tbpgr_utils' bulk_puts_eval binding, <<-EOS /h.ge/i.source /h.ge/im.source //.source EOS __END__ 下記はTbpgrUtils gemの機能 bulk…

Ruby | Regexp | options

概要 Regexp#options -> Integer 詳細 規表現の生成時に指定されたオプションを返却。 戻り値は Regexp::EXTENDED, Regexp::IGNORECASE, Regexp::MULTILINE, Regexp::FIXEDENCODING, Regexp::NOENCODING, の論理和。 サンプルコード require 'tbpgr_utils' b…

Ruby | Regexp | names

概要 Regexp#names -> [String] 詳細 正規表現に含まれる名前付きキャプチャ(named capture)の名前を 文字列の配列で返却。 サンプルコード require 'tbpgr_utils' bulk_puts_eval binding, <<-EOS /(?<name>.+?), (?<age>.+?)/.names /(?<name>.+?), (?<name>.+?), (?<age>.+?)/.names /</age></name></name></age></name>…

Ruby | Regexp | named_captures

概要 Regexp#named_captures 詳細 正規表現に含まれる名前付きキャプチャ(named capture)の情報を Hash で返却。 Hash のキーは名前付きキャプチャの名前で、値は index のリストを返却。 サンプルコード require 'tbpgr_utils' bulk_puts_eval binding, <<-…

Ruby | Regexp | match

概要 Regexp#match(str, pos = 0) -> MatchData | nil Regexp#match(str, pos = 0) {|m| ... } -> object | nil 詳細 指定された文字列 str に対して 位置 pos からマッチングを行う。マッチした場合: MatchData オブジェクトを返却 マッチしなかった場合: n…

Ruby | Regexp | fixed_encoding?

概要 Regexp#fixed_encoding? -> bool 詳細 正規表現が任意の ASCII 互換エンコーディングとマッチ可能な時に false を返却。 サンプルコード require 'tbpgr_utils' bulk_puts_eval binding, <<-EOS /ascii/.fixed_encoding? /utf-8/u.fixed_encoding? /ほ…

Ruby | Regexp | encoding

概要 Regexp#encoding -> Encoding 詳細 正規表現オブジェクトのエンコーディングを表す Encoding オブジェクト を返却。 サンプルコード require 'tbpgr_utils' bulk_puts_eval binding, <<-EOS /h.ge/.encoding /h.ge/u.encoding /ほげ/.encoding EOS __EN…

Ruby | Regexp | casefold?

概要 Regexp#casefold? 詳細 正規表現が大文字小文字の判定をしないようにコンパイルされている時、 真を返却。 サンプルコード require 'tbpgr_utils' bulk_puts_eval binding, <<-EOS /h.ge/i.casefold? /h.ge/.casefold? Regexp.compile("h.ge", Regexp::…

Ruby | Regexp | ~

概要 Regexp#- 詳細 以下と同じ self =~ $_ サンプルコード require 'tbpgr_utils' $_ = "hogehigehage" ~ /(h.ge)(h.ge)(h.ge)/ bulk_puts_eval binding, <<-EOS Regexp.last_match Regexp.last_match(0) Regexp.last_match(1) Regexp.last_match(2) Regexp…

Ruby | Regexp | =~

概要 Regexp#=~ string -> Fixnum | nil 詳細 文字列 string との正規表現マッチを行います。マッチした場合 |> マッチした位置のインデックスを返却 マッチしなかった場合 |> nil を返却。 String が nil の場合 |> nil を返却。 サンプルコード require 't…

Ruby | Regexp | ===

概要 Regexp#=== string -> bool 詳細 文字列 string との正規表現マッチを行い、 マッチした場合は真を返却します。 string が文字列でもシンボルでもない場合には false を返します。るりまには記載されていないが、 Regexp#=== は case 文の内部での利用…

Ruby | Regexp | == / eql?

概要 Regexp#self == other -> bool Regexp#eql?(other) -> bool 詳細 otherが同じパターン、オプション、文字コードの正規表現であったらtrueを返却。 サンプルコード require 'tbpgr_utils' bulk_puts_eval binding, <<-EOS /h.ge/ == /h.ge/ /h.ge/ == Re…

Ruby | Regexp | union

概要 Regexp.union(*pattern) -> Regexp 詳細 引数として与えた pattern を選択 | で連結し、Regexp として返却。 結果の Regexp は与えた pattern のどれかにマッチする場合にマッチするものになります。 サンプルコード require 'tbpgr_utils' bulk_puts_e…

Ruby | Regexp | try_convert

概要 Regexp#try_convert(obj) -> Regexp | nil 詳細 obj を to_regexp メソッドで Regexp オブジェクトに変換する。 成功すれば Regexp を返却。 失敗時は nil を返却。 サンプルコード require 'tbpgr_utils' class DuckTypeRegexp def initialize(pattern…

Ruby | Regexp | last_match

概要 Regexp#last_match -> MatchData Regexp#last_match(nth) -> String | nil 詳細 カレントスコープで最後に行った正規表現マッチの MatchData オ ブジェクトを返却。 $~ と同じ。RuboCop では $~ よりも、こちらが推奨される。 引数 nth 付きの場合は、 …

Ruby | Regexp | escape

概要 Regexp#escape(string) -> String Regexp#quote(string) -> String 詳細 正規表現の利用時にエスケープが必要な文字にバックスラッシュを付与する。 サンプルコード puts Regexp.escape('.*?/+-{}()abc') puts Regexp.quote('.*?/+-{}()abc') 出力 \.\*…

Ruby | Regexp | compile

概要 Regexp#compile(string, option = nil, code = nil) -> Regexp Regexp#new(string, option = nil, code = nil) -> Regexp 詳細 文字列 string をコンパイルして正規表現オブジェクトを生成して返却。 第一引数が正規表現であれば第一引数を複製して返し…