A
A
Anton2016-07-15 19:35:36
Ruby on Rails
Anton, 2016-07-15 19:35:36

How to write a method with "do" in Ruby?

How to write an analog to this?

<%= content_tag :div, class: "strong" do %>
  Hello world!
<% end %>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Mikhail Osher, 2016-07-15
@hummingbird

As far as I remember, it's &block.
// EDIT
apidock.com/rails/ActionView/Helpers/TagHelper/con...

# File actionview/lib/action_view/helpers/tag_helper.rb, line 103
      def content_tag(name, content_or_options_with_block = nil, options = nil, escape = true, &block)
        if block_given?
          options = content_or_options_with_block if content_or_options_with_block.is_a?(Hash)
          content_tag_string(name, capture(&block), options, escape)
        else
          content_tag_string(name, content_or_options_with_block, options, escape)
        end
      end

apidock.com/rails/ActionView/Helpers/CaptureHelper...
# File actionview/lib/action_view/helpers/capture_helper.rb, line 36
      def capture(*args)
        value = nil
        buffer = with_output_buffer { value = yield(*args) }
        if string = buffer.presence || value and string.is_a?(String)
          ERB::Util.html_escape string
        end
      end

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question