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

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 (100)
1 4 11
edit history

Your answer


Privacy: Your email address will only be used for sending these notifications.
Anti-spam verification:
By submitting this post you agree to our Terms & Conditions.
To avoid this verification in future, please log in or register.

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 (100)
1 4 11
edit history