중고 신입이 개발해보기..

ruby block 본문

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

'Back-End > Ruby On Rails' 카테고리의 다른 글

after_update와 after_save의 차이는?  (0) 2024.01.23
Rails로 서버 만들기  (0) 2023.04.25
Rails 6 Multiple database  (0) 2022.11.29
[Ruby on Rails : Heroku] 무료서버만들기  (0) 2022.03.23
레일즈 디비 설정 변경  (0) 2022.03.17
Comments