3
\$\begingroup\$

I can see a possible performance optimization with declaring 2 arrays and iterating the $fake_widgets only once, but how else can I make the following conditional string building more elegant in Ruby?

pending_coloring = $fake_widgets.select do |widget|
  widget[:status] == 'awaiting_coloring' && widget[:owner_color] == @owner.id
end

pending_processing = $fake_widgets.select do |widget|
  widget[:status] == 'awaiting_processing' && widget[:owner_color] == @owner.id
end

notification_text = "You have "

if pending_coloring.size == 1
  notification_text += "#{pending_coloring.size} widget to color"
elsif pending_coloring.size > 1
  notification_text += "#{pending_coloring.size} widgets to color"
end

if pending_coloring.size > 0 && pending_processing.size > 0
  notification_text += " and "
end

if pending_processing.size == 1
  notification_text += "#{pending_processing.size} widget to process"
elsif pending_processing.size > 1
  notification_text += "#{pending_processing.size} widgets to process"
end

notification_text += ". Go to widget dashboard"
\$\endgroup\$
3
\$\begingroup\$

First of all, you have weird behaviour if pending_coloring and pending_processing are both empty arrays. The output would then be

You have . Go to widget dashboard

I assume that in such a case, it should just say "Go to widget dashboard".


I think that pluralization ought to be treated as an issue. Even if your application happens to be English-only for now, it's actually easier to generalize the solution, since pluralization is considered a solved problem in internationalization. In particular, you can use the i18n gem (which is included in Rails, and is a simple installation with no dependencies even if you aren't using Rails).

require 'i18n'

pending_coloring = …
pending_processing = …

I18n.backend.store_translations :en, 'widget' => {
    :zero => nil,
    :one => '1 widget to %{verb}',
    :other => '%{count} widgets to %{verb}'
}
coloring_text = I18n.t 'widget', :count => pending_coloring.size, :verb => 'color'
processing_text = I18n.t 'widget', :count => pending_processing.size, :verb => 'process'

notification_text = if (!coloring_text && !processing_text) then
  ''
else
  "You have #{[coloring_text, processing_text].compact.join(' and ')}.  "
end + "Go to widget dashboard"
\$\endgroup\$
  • \$\begingroup\$ Both answers were very helpful to show me better alternatives to the lengthy code I had. I have awarded this one as it more comprehensively dealt with the sample code from question. \$\endgroup\$ – ljs.dev Nov 4 '15 at 3:02
1
\$\begingroup\$

This is a pattern to generate conditional concatenation:

text = [
  "This is",
  complexity == :simple ? "a simple example" : "an example",
  "of conditional string concatenation.",
  ("Bye!" if must_greet),
].compact.join(" ")
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.