I'm currently reading Agile Web Development with Rails 5 and following steps to create simple book store web. It is basic rails app but I'm actually suprised the fact that how things the author do differently. I also enjoy every page and learn new tricks and rails' way of doing things! There are so much to learn but here are some of things that I've learned.
The task is to creating helper method hidden_div_itemsto render partial if there is a cart.
First, I haven't used content_tag before so that's something new. If you aren't familier with this, click here. Basically, it will create html tag for you. You can pass the tag as symbol ':div', or string "div". If you look at the apidock, the last arg of the content_tag is '&block'.
By definition, it "Returns an HTML block tag of type name surrounding the content. Add HTML attributes by passing an attributes hash to options."
Now, let's get into '&block' and 'yield' busineses.
Both print_myself will call the &block with arg. When you call the function, your block looks kind of funny. It looks like completely different entity from arg. BUT this is a part of attributes for the print_myself.
In the above example of hidden_div_if, it looks even weirder because it looks like we are passing 2 arguments - @cart.line_items.empty? and id: 'cart' and the hidden_div_if is also taking 2 arguments and make attributes as an empty hash by default. So here is what's going on: