erb idiom to handle undefined variable
erb, ruby
Solution
I would do this:
<% if defined?(environment) %>
<% Array(environment).each do |f| %>
one line: <%= f %>
<% end %>
<% end %>
I didn't understand why you joining on new lines and then splitting on them again, so I removed it from the example.
Problem
I'm trying to write some puppet .erb, I'd like to handle this "environment" variable if it's: - undefined - a string with newlines - an array. I've got as far as this: ``` <% Array(environment).join("\n").split(%r{\n}).each do |f| %> one line: <%= f %> <% end %> ``` But haven't gotten around the undefined case yet. I've tried this ``` <% if (defined?(environment)).nil? %? <% Array(environment).join("\n").split(%r{\n}).each do |f| %> one line: <%= f %> <% end %> <% end %> ``` but am still getting "(erb):11: undefined local variable or method `environment' for main:Object (NameError)" when trying to test it like this: ``` ruby -rerb -e "environmentUNDEFINEME= [ 'cronvar=cronval', 'var2=val2' ]; puts ERB.new(File.read('templates/job.erb')).result" ``` Sorry this is so basic, but somebody's got to ask the easy questions. Any help?