Tbpgr Blog

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

Ruby | Proc | new

概要

Proc.new -> Proc
Proc.new { ... } -> Proc

詳細

ブロックをコンテキストとともにオブジェクト化して返却。
ブロックを指定しない場合は、このメソッドを呼び出したメソッド
ブロックを伴う場合に、Procを生成。

サンプルコード
require 'tbpgr_utils'

def not_use_block_case(msg)
  pr = Proc.new
  pr.call(msg * 2)
end

pr = Proc.new { |msg| msg * 2 }

bulk_puts_eval binding, <<-EOS
not_use_block_case('hoge') {|arg| arg }
pr.call('hige')
EOS

__END__
下記はTbpgrUtils gemの機能
bulk_puts_eval

https://rubygems.org/gems/tbpgr_utils
https://github.com/tbpgr/tbpgr_utils

出力

not_use_block_case('hoge') {|arg| arg } # => "hogehoge"
pr.call('hige')                         # => "higehige"