Rendering block content from erb

If you want to create any methods which returns block content and you calling this methods in erb (slim, haml, etc) — you can have a big problems. For example — you can see repetition of content in your browser or even worse.

Head tag after calling in erb

Let our methods are as follows:

def add_to_head(&block)
  @head_html ||= []
  @head_html << block if block_given?
def display_custom_head
  return unless @head_html

You can use capture method which included to rails and sinatra-contrib librares.

But what if you can not use these methods, or they have too much overhead for a current task?

Such a case arose in my pull request in sidekiq, where I wrote a simple variant of the capture for erb files:

def capture(&block)
  eval(‘’, block.binding)

I hope it will be useful. Happy hacking!

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.