Ruby On Rails 면접 질문과 답변
Question: What is the purpose of YIELD in Ruby on Rails?Answer:The interpreter essentially invokes a separate piece of code and places it in the location. You might say it is similar to a method calling another method. Lets understand a little bit of background about where YIELD might be useful first. The Rails framework encourages you to write code that is DRY (Dont Repeat Yourself). Developers often write common code in a central file and then they write the custom code in the specific files. Lets say you are building a web application and you want all pages to have a common header, a common footer, the same Welcome user-name! message. You can put all this common code in your application.html.erb file. <html> .... common page title
.. standard header...
<body>
..common page title,
<%= YIELD %>
..footer code can go here... </body>
</html>
The rest of the custom code can go in your specific file. Say the page you are creating is the list of articles. Then in your implementation file you would just write the code for pulling in the articles and the final page displayed to the user would be your custom code which will be placed instead of the <%= YIELD %>code in the application.html.erb file. |
복습용 저장
이 항목을 북마크하거나, 어렵게 표시하거나, 복습 세트에 넣을 수 있습니다.
Most helpful rated by users:
- What is Ruby On Rails?
- Why Ruby on Rails?
- Explain how (almost) everything is an object in Ruby.
- What are Gems and which are some of your favorites?
- How would you declare and use a constructor in Ruby?