Tbpgr Blog

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

Ruby

Ruby | Range | step

概要 Range#step(s = 1) {|item| ... } -> self Range#step(s = 1) -> Enumerator 詳細 範囲内の要素を s おきに繰り返却 サンプルコード require 'tbpgr_utils' class Hoge attr_accessor :count def initialize(count) @count = count end def succ copy =…

Ruby | Range | min

概要 Range#min -> object | nil Range#min {|a, b| ... } -> object | nil 詳細 範囲内の最小の値を返却。 範囲に要素がなければ nil。ブロック付の場合、 ブロックの評価結果で範囲内の各要素の大小判定を行い、最小の要素を返却。 範囲に要素がなければ n…

Ruby | Range | max

概要 Range#max -> object | nil Range#max {|a, b| ... } -> object | nil 詳細 範囲内の最大の値を返却。 範囲に要素がなければ nil。ブロック付の場合、 ブロックの評価結果で範囲内の各要素の大小判定を行い、最大の要素を返却。 範囲に要素がなければ n…

Ruby | Range | exclude_end?

概要 Range#exclude_end? -> bool 詳細 範囲オブジェクトが終端を含まないとき真を返却 サンプルコード require 'tbpgr_utils' bulk_puts_eval binding, <<-EOS (2..5).exclude_end? (2...5).exclude_end? Range.new(2, 5).exclude_end? Range.new(2, 5, fal…

Ruby | Range | eql?

概要 Range#eql?(other) -> bool 詳細 以下の条件を満たせば真を返却 ・ 指定された other が Range クラスのインスタンス ・ 始点と終点が eql? メソッドで比較して等しい ・ Range#exclude_end? が同じ サンプルコード require 'tbpgr_utils' bulk_puts_ev…

Ruby | Range | end or last

概要 Range#end -> object Range#last -> object Range#last(n) -> [object] 詳細 終端の要素を返却。 last に引数 n を付加すると、終端から n 要素を返却 サンプルコード require 'tbpgr_utils' bulk_puts_eval binding, <<-EOS (2..5).end (2...5).end (2…

Ruby | Range | each

概要 Range#each {|item| ... } -> self Range#each -> Enumerator 詳細 範囲内の要素に対して繰り返します。 Range#each は各要素の succ メソッドを使用してイテレーションする succ メソッドを持たない場合、エラーになります。 サンプルコード require '…

Ruby | Range | cover?

概要 Range#cover?(obj) -> bool 詳細 obj が範囲内に含まれている時に真を返却。 Range#include? ( member?, === ) と異なり メソッドによる演算により範囲内かどうかを判定。include? => 離散値 cover? => 連続値ただし、数値は例外として include? も連続…

Ruby | Range | begin or first

概要 Range#begin -> object Range#first -> object Range#first(n) -> [object] 詳細 始端の要素を返却 サンプルコード require 'tbpgr_utils' bulk_puts_eval binding, <<-EOS (2..5).begin (2..5).first (2..5).first(2) (2..1).first (2..1).first(1) EO…

Ruby | Range | === or include? or member?

概要 Range#=== obj -> bool Range#include?(obj) -> bool Range#member?(obj) -> bool 詳細 obj が範囲内に含まれている時に真を返却。 Range#=== は case 文で使用。 サンプルコード require 'tbpgr_utils' [:include?, :member?, :===].each do |m| p "**…

Ruby | Range | ==

概要 Range#== other -> bool 詳細 指定された other が Range クラスのインスタンスであり、 始点と終点が == メソッドで比較して等しく、 Range#exclude_end? が同じ場合に true を返却。 サンプルコード require 'tbpgr_utils' bulk_puts_eval binding, <…

Ruby | Range | new

概要 Range.new(first, last, exclude_end = false) -> Range 詳細 first から last までの範囲オブジェクトを生成して返却。 exclude_end を true にした場合、終端を含まない。 つまり、exclude_end = false は first..last exclude_end = true は first..…

Ruby | Proc | to_s

概要 Proc#to_s -> String 詳細 self の文字列表現を返却。 サンプルコード p Proc.new {}.to_s {}.to_s 出力 "#<Proc:0x2a8b770@/path/to/your/script.rb:1>" 参照 http://docs.ruby-lang.org/ja/2.1.0/method/Proc/i/to_s.html</proc:0x2a8b770@/path/to/your/script.rb:1>

Ruby | Proc | to_proc

概要 Proc#to_proc -> self 詳細 自身を返却。 Duck Typing 用か? サンプルコード require 'tbpgr_utils' p1 = Proc.new {} p2 = proc {} p3 = lambda {} class Sum def sum(a, b) a + b end def to_proc -> (a, b) {a + b} end end def use_proc(a, b, pr)…

Ruby | Proc | source_location

概要 Proc#source_location -> [String, Fixnum] | nil 詳細 Procを定義したソースコードのファイル名と行番号を配列で返却。 サンプルコード require 'tbpgr_utils' pr1 = Proc.new {} pr2 = proc {} lm = lambda {} bulk_puts_eval binding, <<-EOS pr1.so…

Ruby | Proc | lambda?

概要 Proc#lambda? -> bool 詳細 手続きオブジェクトの引数の取扱が厳密であるならば true を返却。 サンプルコード require 'tbpgr_utils' def hoge(&b) b.lambda? end def hige;;end bulk_puts_eval binding, <<-EOS lambda {}.lambda? Proc.new {}.lambda…

Ruby | Proc | curry

概要 Proc#curry 詳細 Procをカリー化する。カリー化を利用すると、1引数をとる高階関数に複数の引数を持つ関数を渡せる。下記の記事が非常に参考になる。 「関数型Ruby」という病(3) - カリー化(Proc#curry, Proc#flip) http://yuroyoro.hatenablog.com/ent…

Ruby | Proc | arity

概要 Proc#arity 詳細 Proc オブジェクトが受け付ける引数の数を返却。 可変長の場合は必要とされる引数の数 + 1 を負の値にして返却。 サンプルコード require 'tbpgr_utils' bulk_puts_eval binding, <<-EOS Proc.new { || }.arity Proc.new { |x|x }.arit…

Ruby | Proc | []

概要 Proc#self[*arg] -> () Proc#call(*arg) -> () Proc#self === *arg -> () Proc#yield(*arg) -> () 詳細 手続きオブジェクトを実行してその結果を返却。 「 === 」 は case 文での利用のため。 サンプルコード require 'tbpgr_utils' pr = Proc.new { |m…

Ruby | Proc | new

概要 Proc.new -> Proc Proc.new { ... } -> Proc 詳細 ブロックをコンテキストとともにオブジェクト化して返却。 ブロックを指定しない場合は、このメソッドを呼び出したメソッドが ブロックを伴う場合に、Procを生成。 サンプルコード require 'tbpgr_util…

Ruby | ObjectSpace | undefine_finalizer

概要 Object#undefine_finalizer(obj) -> object 詳細 define_finalizer で登録した obj が解放されるときに実行されるファイナライザ proc の登録をすべて解除する。 サンプルコード class Hoge def Hoge.callback proc { puts "finalize" } end def initia…

Ruby | ObjectSpace | garbage_collect

概要 ObjectSpace#garbage_collect(full_mark: true, immediate_sweep: true) -> nil 詳細 参照されなくなったオブジェクトを回収する。 GC.start と同じ。 サンプルコード require 'tbpgr_utils' bulk_puts_eval binding, <<-EOS GC.count ObjectSpace.garb…

Ruby | ObjectSpace | each_object

概要 ObjectSpace#each_object {|object| ...} -> Integer ObjectSpace#each_object(klass) {|object| ...} -> Integer ObjectSpace#each_object -> Enumerator ObjectSpace#each_object(klass) -> Enumerator 詳細 指定された klass と Object#kind_of? の…

Ruby | ObjectSpace | define_finalizer

概要 ObjectSpace#define_finalizer(obj, proc) -> Array ObjectSpace#define_finalizer(obj) {|id| ...} -> Array 詳細 obj が解放されるときに実行されるファイナライザ proc を 登録。 同一オブジェクトへの設定は追加になる。 サンプルコード class Hoge…

Ruby | ObjectSpace | count_objects

概要 ObjectSpace#count_objects(result_hash = {}) -> Hash 詳細 オブジェクトを種類ごとにカウントした結果を Hash として返却。 サンプルコード require 'tbpgr_utils' hash = {} bulk_puts_eval binding, <<-EOS ObjectSpace.count_objects class Hoge; …

Ruby | ObjectSpace | _id2ref

概要 ObjectSpace#_id2ref(id) -> object 詳細 オブジェクト ID(Object#__id__)からオブジェクトを取得 サンプルコード require 'tbpgr_utils' Person = Struct.new(:name, :age) tanaka = Person.new('tanaka', 34) hoge = "hoge" bulk_puts_eval binding, <…

Ruby | Object | remove_instance_variable

概要 Object#remove_instance_variable(name) -> object 詳細 オブジェクトからインスタンス変数 name を削除。 そのインスタンス変数に設定されていた値を返却。 サンプルコード require 'tbpgr_utils' class Person attr_reader :name, :age def initializ…

Ruby | Object | initialize

概要 Object#initialize(*args, &block) -> object 詳細 ユーザ定義クラスのオブジェクト初期化メソッド。 new から呼び出される処理で、 new に渡された引数をそのまま引き継ぐ。 ユーザーが作成するクラスで再定義されることを期待されている。 サンプルコ…

Ruby | Object | untrusted?

概要 Object#untrusted? -> bool 詳細 オブジェクトに「untrustマーク」がついていたら真を返却する サンプルコード require 'tbpgr_utils' Person = Struct.new(:name, :age) tanaka = Person.new('tanaka', 24) bulk_puts_eval binding, <<-EOS tanaka.nam…

Ruby | Object | untaint

概要 Object#untaint -> self 詳細 オブジェクトの汚染マークを除去する。セキュリティレベルについては下記参照。 http://docs.ruby-lang.org/ja/2.1.0/doc/spec=2fsafelevel.html サンプルコード require 'tbpgr_utils' name = ARGV[0] age = ARGV[1] Pers…