I am working with a hash that looks something like this:
h = {
"side1"=>["green", "Green"],
"side2"=>["verde", "Verde"]
}
However, it may only have one item in it:
h = {
"side2"=>["verde", "Verde"]
}
I would like to output the hash so that it looks something like this:
User changed side1 from green to Green and side2 from verde to Verde
When there are two items will be separated from an “and”. Otherwise it will look like this:
User changed side2 from verde to Verde
This is all happening in my rails view script. How can I write the code to be able to do this?
You could do something like this:
So, in an ERB view:
If you were doing this sort of thing a lot then you might want to build a helper for it.
You could also use
to_sentencesince you’re in Rails:The
to_sentenceapproach also scales up to more than two elements better. So, if you also had"side3"=>["pancakes", "Pancakes"], then you’d get: