diff --git a/README.md b/README.md index 38983aa..a0fcf5d 100644 --- a/README.md +++ b/README.md @@ -224,6 +224,7 @@ smart people at [Hashrocket](http://hashrocket.com/). - [Question Mark Operator](ruby/question-mark-operator.md) - [Rake Only Lists Tasks With Descriptions](ruby/rake-only-lists-tasks-with-descriptions.md) - [Replace The Current Process With An External Command](ruby/replace-the-current-process-with-an-external-command.md) +- [Rendering ERB](ruby/rendering-erb.rb) - [Safe Navigation Operator](ruby/safe-navigation-operator.md) - [Set RVM Default Ruby](ruby/set-rvm-default-ruby.md) - [Squeeze Out The Extra Space](ruby/squeeze-out-the-extra-space.md) diff --git a/ruby/rendering-erb.md b/ruby/rendering-erb.md new file mode 100644 index 0000000..7f6caf1 --- /dev/null +++ b/ruby/rendering-erb.md @@ -0,0 +1,32 @@ +# Rendering ERB + +If you have a string that contains ERB templating, you can quickly generate +the resulting string with the following code snippet: + +```ruby +require 'erb' + +some_template_string = <<-TEXT +The top +<% 5.times do |i| %> +Item <%= i + 1 %> +<% end %> +The bottom +TEXT + +puts ERB.new(some_template_string).result +``` + +This will print the following to stdout: + +``` +The top +Item 1 +Item 2 +Item 3 +Item 4 +Item 5 +The bottom +``` + +[source](http://www.stuartellis.eu/articles/erb/)