Created
October 10, 2014 15:16
-
-
Save jadon1979/2ce634b6c28e6b6c74c2 to your computer and use it in GitHub Desktop.
Another block example for you
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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