Sen gerçekten Sonunda çağırabilir oluşturulan iç içe lambdas veya procs için inject
kullanabilirsiniz. Eğer with_this
sarın Eğer
def in_nested_contexts(&blk)
[:with_this, :with_that, :and_in_this_context].reverse.inject(blk) {|block, symbol|
->{ send symbol, &block }
}.call
end
: Size diziyi ters ve başlangıç değeri olarak bu bloğu kullanın, ardından inject gelen sonucun etrafında birbirini izleyen her işlevi sarmak, böylece yuvanın iç olmak için verilen blok ihtiyaç , ve diğer yöntemlerle puts
ifadelerinden önce ve sonra, bunu şu eylemde görebilirsiniz:
in_nested_contexts { puts "hello, world" }
#=>
with_this start
with_that start
context start
hello, world
context end
with_that end
with_this end
Sadece neye ihtiyacım var, teşekkürler! – Julik