I am having some problems creating a loop that could show the cheapest price.
Here is my controller:
def domain
country_codes = ['.dk', '.com', '.eu', '.net', '.org', '.biz', '.info', '.nu', '.name', '.se', '.fi', '.net', '.de', '.it'] # etc. could move this to a config if needed
@domain = params[:domain]
@results = {}
country_codes.each do |cc|
@results[cc] = Whois.whois(@domain + cc)
end
@pricedk = Domain.sort("dk ASC").first
@pricecom = Domain.sort("com ASC").first
@priceorg = Domain.sort("org ASC").first
@pricenet = Domain.sort("net ASC").first
#ETC...
end
My view:
<table border="0" bordercolor="#FFCC00" width="700" cellpadding="0" cellspacing="0">
<tr class="top">
<td class="checkdomain"></td>
<td>Name</td>
<td>Domain</td>
<td style="font-size:9px;"></td>
</tr>
<% @results.each_pair do |country_code, available| %>
<% klass = available.registered? ? "pinfo" : "info" %>
<tr>
<td><span class="<%= klass %>"></span></td>
<td><%= @domain + country_code %></td>
<td>PRICE HERE</td>
</tr>
<% end %>
</table>
I want to DRY the @pricecom, @pricedk, @priceorg up.
How do I include it in the @results loop?
When you have repetition like this, with many different instance variables, what you need to do is roll them all up into a single instance variable that’s a Hash:
You’ll see this pattern every so often.
@pricesdkbecomes@prices[:dk]