Back-End /Ruby On Rails
ruby block
rootkaien
2022. 11. 30. 15:38
ruby코드를 보는중..
yield if block_given?
라는것이 있다..
https://smartbase.tistory.com/54 참고
Ruby 처음 배우기 : Block & Yield
Blocks 블록은 클로저 혹은 익명함수와 비슷합니다. 즉 이름 없는 메서드를 생성하는 방법이라고 할 수 있으며, do...end 혹은 {} 로 정의합니다. 블록은 처리를 하나의 단위로 묶은 것으로, 메서드
smartbase.tistory.com
def block_given_sample
puts "before yield"
yield if block_given?
puts "after yield"
end
메서드가 있다..
block_given_sample
실행하면...
before yield
after yield
만 나오지만
block_given_sample do
puts "block"
end
로 실행하면..
yield if block_given? 부분에서 puts "block" 이 실행된다.
before yield
block
after yield