Skip to content

Instantly share code, notes, and snippets.

@jadon1979
Created October 10, 2014 15:16
Show Gist options
  • Select an option

  • Save jadon1979/2ce634b6c28e6b6c74c2 to your computer and use it in GitHub Desktop.

Select an option

Save jadon1979/2ce634b6c28e6b6c74c2 to your computer and use it in GitHub Desktop.
Another block example for you
def create_tag(tag, **args)
attributes = args.map { |k,v| " #{k}=\"#{v}\"" }.join
block = yield if block_given?
"<%s%s>%s</%s>" % [tag, attributes, block, tag]
end
link_1 = create_tag(:a, {href: "#", title: "My link"})
link_2 = create_tag(:a, {href: "#", title: "My link"}) do
'Link Text'
end
link_3 = create_tag(:a, {href: "#", title: "My link"}) do
create_tag(:span) do
'inside text'
end
end
puts link_1
#=> <a href="#" title="My link"></a>
puts link_2
#=> <a href="#" title="My link">Link Text</a>
puts link_3
#=> <a href="#" title="My link"><span>inside text</span></a>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment