Guidelines

This site is for tech Q&A. Please keep your posts focused on the subject at hand.

Ask one question at a time. Don't conflate multiple problems into a single question.

Make sure to include all relevant information in your posts. Try to avoid linking to external sites.

Links to documentation are fine, but in addition you should also quote the relevant parts in your posts.

0 votes
193 views
193 views

In an ERB template I want to render a hash into a list of config settings where strings need to be in quotes whereas values of other types don't. The template looks like this:

<%- @myhash.each_pair do |key, val| -%>
<%= key %> = <% if XXXXX %>"<%= val %>"<% else %><%= val %><% end %>
<%- end -%>

But what do I put as the condition (XXXXX)?

in Scripting
by (115)
2 19 33
edit history

Please log in or register to answer this question.

1 Answer

0 votes
 

Use the is_a? method to check if an object is of a particular type:

<%= key %> = <% if val.is_a? String %>"<%= val %>"<% else %><%= val %><% end %>

The method will return true if the object is of the given type (in this case String) or any class derived from that type.

If you want an exact match (i.e. not match subclasses) use the instance_of? method instead.


edited by
by (115)
2 19 33
edit history
...